Skip to content

CHORE: Replace parser.ParseDir with golang.org/x/tools/go/packages#4195

Open
philpennock wants to merge 1 commit intoStackExchange:mainfrom
PennockTech:pdp/replace-parser-parsedir
Open

CHORE: Replace parser.ParseDir with golang.org/x/tools/go/packages#4195
philpennock wants to merge 1 commit intoStackExchange:mainfrom
PennockTech:pdp/replace-parser-parsedir

Conversation

@philpennock
Copy link
Copy Markdown
Contributor

parser.ParseDir is deprecated since Go 1.25. Use packages.Load with NeedTypes and NeedTypesInfo to correctly extract CanUse* constant integer values via TypesInfo.Defs, preserving the test's ability to detect missing providerCapabilityChecks entries.


This is a replacement PR for #4154. I took my review feedback from that PR, copied the markdown into two local files, and prompted Claude Code (using Sonnet 4.6) with:

Branch sx-pr/4154 contains one commit, which purports to "Replace parser.ParseDir, which is deprecated". But it is broken. This is a test of the codebase, via AST checks, that various providers.CanUse* checks are called appropriately. Files local.pdp.review1.1 and local.pdp.review1.2 (git-ignored) contain my review feedback, pointing out how the change is broken and how it can be validated. You are on a new git branch based off current main; please provide a correct replacement, and validate that it works.

The next section is choice quotes from Claude's analysis while working, and the final section was Claude's summary at end-of-work.

I have independently reviewed the code, it looks good, and I've validated that the tests look reasonable and correctly fail when a test is commented out (to ensure the AI did not fabricate that). I think this is a sane way forward.

I thought it would be amusing to use Claude to critique Claude's earlier work.


Now I understand the problem clearly. The broken PR sets capabilityInts[name.Name] = 0 for all constants (the comment even admits it), which means every CanUse* constant maps to int 0, and since capIntsToNames[0] finds the first entry, all checks pass trivially. The test is completely neutered.

The fix is to use packages.NeedTypes/NeedTypesInfo (already requested in the config) to actually read the integer values of the constants. Let me look at the providers package to understand the constants:

[...]

The problem is clear. The broken PR:

  1. Uses packages.Load correctly to get type info (NeedTypes, NeedTypesInfo)
  2. But then abandons the type info and sets all capabilityInts to 0, making every CanUse* constant map to CanAutoDNSSEC (which is in capIntsToNames[0]), so all checks pass trivially

The fix is to use providers.TypesInfo.Defs to look up the actual *types.Const value for each constant. Let me write the corrected test file:


Both validations from the review pass:

  1. Correct output: integer values are real ((4), (9), etc.) and each is paired with its correct record type — matches the "before" output exactly.
  2. Test actually catches omissions: commenting out the DNAME entry causes FAIL with MISSING: providers.CanUseDNAME (9).

What was wrong in the PR: The code switched from parser.ParseDir to packages.Load correctly, but the author gave up on extracting real integer values and wrote capabilityInts[name.Name] = 0 for everything. Since all constants got value 0, they all matched capIntsToNames[0] = AUTODNSSEC, making the test trivially pass regardless of what was in providerCapabilityChecks.

The fix: Instead of iterating the raw AST and guessing values, use providers.TypesInfo.Defs (which is what NeedTypesInfo provides) to look up each identifier's *types.Const object and call constant.Int64Val(c.Val()) to get the actual iota integer.

parser.ParseDir is deprecated since Go 1.25. Use packages.Load with
NeedTypes and NeedTypesInfo to correctly extract CanUse* constant
integer values via TypesInfo.Defs, preserving the test's ability to
detect missing providerCapabilityChecks entries.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant