diff --git a/.github/workflows/unused-imports.yml b/.github/workflows/unused-imports.yml new file mode 100644 index 000000000..a6b50c9ae --- /dev/null +++ b/.github/workflows/unused-imports.yml @@ -0,0 +1,134 @@ +name: CI / Unused Imports Check + +on: + pull_request: + branches: [main] + workflow_dispatch: + +jobs: + check-unused-imports: + runs-on: ubuntu-latest + permissions: + contents: read + pull-requests: write + + steps: + - name: Checkout PR code + uses: actions/checkout@v3 + with: + fetch-depth: 0 + + - name: Setup Zig + uses: mlugg/setup-zig@v2 + with: + version: 0.15.1 + + - name: Build zigimports + run: | + git clone --depth 1 https://github.com/tusharsadhwani/zigimports.git /tmp/zigimports + cd /tmp/zigimports + zig build --release=safe + # Binary is named zigimports--, find and symlink it + ln -s /tmp/zigimports/zig-out/bin/zigimports-* /tmp/zigimports/zig-out/bin/zigimports + + - name: Get changed Zig files + id: changed-files + run: | + FILES=$(git diff --name-only --diff-filter=d ${{ github.event.pull_request.base.sha }} ${{ github.event.pull_request.head.sha }} | grep '\.zig$' || true) + echo "files<> $GITHUB_OUTPUT + echo "$FILES" >> $GITHUB_OUTPUT + echo "EOF" >> $GITHUB_OUTPUT + + - name: Check for unused imports + id: check + run: | + RESULTS="" + echo "${{ steps.changed-files.outputs.files }}" | while read file; do + if [ -n "$file" ] && [ -f "$file" ]; then + /tmp/zigimports/zig-out/bin/zigimports "$file" 2>&1 || true + fi + done | tee unused_imports.txt + + # Set output for later steps + if grep -q "is unused" unused_imports.txt 2>/dev/null; then + echo "has_issues=true" >> $GITHUB_OUTPUT + else + echo "has_issues=false" >> $GITHUB_OUTPUT + fi + + - name: Post PR comment + if: steps.check.outputs.has_issues == 'true' + uses: actions/github-script@v7 + with: + script: | + const fs = require('fs'); + + const content = fs.readFileSync('unused_imports.txt', 'utf8').trim(); + const lines = content.split('\n').filter(l => l.includes('is unused')); + + if (lines.length === 0) return; + + // Check for existing comment and update it + const marker = ''; + const comments = await github.paginate( + github.rest.issues.listComments, + { + owner: context.repo.owner, + repo: context.repo.repo, + issue_number: context.issue.number + } + ); + + const existing = comments.find(c => + c.user.login === 'github-actions[bot]' && + c.body.includes(marker) + ); + + let body = '## ⚠️ Unused Imports Found\n\n'; + body += '```\n' + lines.join('\n') + '\n```\n\n'; + body += 'Run `zigimports --fix ` locally to automatically remove unused imports.\n\n'; + body += marker; + + if (existing) { + await github.rest.issues.updateComment({ + owner: context.repo.owner, + repo: context.repo.repo, + comment_id: existing.id, + body: body + }); + } else { + await github.rest.issues.createComment({ + owner: context.repo.owner, + repo: context.repo.repo, + issue_number: context.issue.number, + body: body + }); + } + + - name: Remove comment if no issues + if: steps.check.outputs.has_issues == 'false' + uses: actions/github-script@v7 + with: + script: | + const marker = ''; + const comments = await github.paginate( + github.rest.issues.listComments, + { + owner: context.repo.owner, + repo: context.repo.repo, + issue_number: context.issue.number + } + ); + + const existing = comments.find(c => + c.user.login === 'github-actions[bot]' && + c.body.includes(marker) + ); + + if (existing) { + await github.rest.issues.deleteComment({ + owner: context.repo.owner, + repo: context.repo.repo, + comment_id: existing.id + }); + } diff --git a/tools/sorcerer/src/RegzWindow.zig b/tools/sorcerer/src/RegzWindow.zig index e9eb20e50..34667b857 100644 --- a/tools/sorcerer/src/RegzWindow.zig +++ b/tools/sorcerer/src/RegzWindow.zig @@ -120,7 +120,6 @@ const Allocator = std.mem.Allocator; const regz = @import("regz"); const VirtualFilesystem = regz.VirtualFilesystem; const RegisterSchemaUsage = @import("RegisterSchemaUsage"); -const diffz = @import("diffz"); const dvui = @import("dvui"); diff --git a/tools/sorcerer/src/main.zig b/tools/sorcerer/src/main.zig index 7d809914b..492bfda91 100644 --- a/tools/sorcerer/src/main.zig +++ b/tools/sorcerer/src/main.zig @@ -1,7 +1,5 @@ const std = @import("std"); -const builtin = @import("builtin"); const dvui = @import("dvui"); -const serial = @import("serial"); const regz = @import("regz"); const schemas = @import("schemas"); const RegisterSchemaUsage = @import("RegisterSchemaUsage");