Skip to content

CHORE: Replace parser.ParseDir, which is deprecated#4154

Closed
tlimoncelli wants to merge 1 commit intomainfrom
tlim_b4089
Closed

CHORE: Replace parser.ParseDir, which is deprecated#4154
tlimoncelli wants to merge 1 commit intomainfrom
tlim_b4089

Conversation

@tlimoncelli
Copy link
Copy Markdown
Contributor

Fixed #4089

FYI: I used Claude to fix this. I actually don't understand the original code so please give this extra scrutiny.

CC @philpennock

Issue

lint is reporting that parser.ParseDir is deprecated:

$ golangci-lint run ./...
pkg/normalize/capabilities_test.go:24:15: SA1019: parser.ParseDir has been deprecated since Go 1.25 and an alternative has been available since Go 1.11: ParseDir does not consider build tags when associating files with packages. For precise information about the relationship between packages and files, use golang.org/x/tools/go/packages, which can also optionally parse and type-check the files too. (staticcheck)
	pkgs, err := parser.ParseDir(fset, providersImportDir, nil, 0)
	             ^

Resolution

Use golang.org/x/tools/go/packages instead.

Copy link
Copy Markdown
Contributor

@philpennock philpennock left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Basic smoke test: invoke go test -v -run TestCapabilitiesAreFiltered ./pkg/normalize

The reported details with this patch are very bogus. "is checked for with ..."

Before:

=== RUN   TestCapabilitiesAreFiltered
    capabilities_test.go:74: ok: providers.CanUseAKAMAICDN (4) is checked for with "AKAMAICDN"
    capabilities_test.go:74: ok: providers.CanUseAKAMAITLC (29) is checked for with "AKAMAITLC"
    capabilities_test.go:74: ok: providers.CanUseAlias (5) is checked for with "ALIAS"
    capabilities_test.go:74: ok: providers.CanUseAzureAlias (6) is checked for with "AZURE_ALIAS"
    capabilities_test.go:74: ok: providers.CanUseCAA (7) is checked for with "CAA"
    capabilities_test.go:74: ok: providers.CanUseDHCID (8) is checked for with "DHCID"
    capabilities_test.go:74: ok: providers.CanUseDNAME (9) is checked for with "DNAME"
    capabilities_test.go:74: ok: providers.CanUseDNSKEY (24) is checked for with "DNSKEY"
    capabilities_test.go:74: ok: providers.CanUseDS (10) is checked for with "DS"
    capabilities_test.go:74: ok: providers.CanUseDSForChildren (11) is checked for with "DS"
    capabilities_test.go:74: ok: providers.CanUseHTTPS (12) is checked for with "HTTPS"
    capabilities_test.go:74: ok: providers.CanUseLOC (13) is checked for with "LOC"
    capabilities_test.go:74: ok: providers.CanUseNAPTR (14) is checked for with "NAPTR"
    capabilities_test.go:74: ok: providers.CanUseOPENPGPKEY (25) is checked for with "OPENPGPKEY"
    capabilities_test.go:74: ok: providers.CanUsePTR (15) is checked for with "PTR"
    capabilities_test.go:74: ok: providers.CanUseRP (17) is checked for with "RP"
    capabilities_test.go:74: ok: providers.CanUseRoute53Alias (16) is checked for with "R53_ALIAS"
    capabilities_test.go:74: ok: providers.CanUseSMIMEA (18) is checked for with "SMIMEA"
    capabilities_test.go:74: ok: providers.CanUseSOA (19) is checked for with "SOA"
    capabilities_test.go:74: ok: providers.CanUseSRV (20) is checked for with "SRV"
    capabilities_test.go:74: ok: providers.CanUseSSHFP (21) is checked for with "SSHFP"
    capabilities_test.go:74: ok: providers.CanUseSVCB (22) is checked for with "SVCB"
    capabilities_test.go:74: ok: providers.CanUseTLSA (23) is checked for with "TLSA"
--- PASS: TestCapabilitiesAreFiltered (0.00s)

On this branch:

=== RUN   TestCapabilitiesAreFiltered
    capabilities_test.go:86: ok: providers.CanUseAKAMAICDN (0) is checked for with "AUTODNSSEC"
    capabilities_test.go:86: ok: providers.CanUseAKAMAITLC (0) is checked for with "AUTODNSSEC"
    capabilities_test.go:86: ok: providers.CanUseAlias (0) is checked for with "AUTODNSSEC"
    capabilities_test.go:86: ok: providers.CanUseAzureAlias (0) is checked for with "AUTODNSSEC"
    capabilities_test.go:86: ok: providers.CanUseCAA (0) is checked for with "AUTODNSSEC"
    capabilities_test.go:86: ok: providers.CanUseDHCID (0) is checked for with "AUTODNSSEC"
    capabilities_test.go:86: ok: providers.CanUseDNAME (0) is checked for with "AUTODNSSEC"
    capabilities_test.go:86: ok: providers.CanUseDNSKEY (0) is checked for with "AUTODNSSEC"
    capabilities_test.go:86: ok: providers.CanUseDS (0) is checked for with "AUTODNSSEC"
    capabilities_test.go:86: ok: providers.CanUseDSForChildren (0) is checked for with "AUTODNSSEC"
    capabilities_test.go:86: ok: providers.CanUseHTTPS (0) is checked for with "AUTODNSSEC"
    capabilities_test.go:86: ok: providers.CanUseLOC (0) is checked for with "AUTODNSSEC"
    capabilities_test.go:86: ok: providers.CanUseNAPTR (0) is checked for with "AUTODNSSEC"
    capabilities_test.go:86: ok: providers.CanUseOPENPGPKEY (0) is checked for with "AUTODNSSEC"
    capabilities_test.go:86: ok: providers.CanUsePTR (0) is checked for with "AUTODNSSEC"
    capabilities_test.go:86: ok: providers.CanUseRP (0) is checked for with "AUTODNSSEC"
    capabilities_test.go:86: ok: providers.CanUseRoute53Alias (0) is checked for with "AUTODNSSEC"
    capabilities_test.go:86: ok: providers.CanUseSMIMEA (0) is checked for with "AUTODNSSEC"
    capabilities_test.go:86: ok: providers.CanUseSOA (0) is checked for with "AUTODNSSEC"
    capabilities_test.go:86: ok: providers.CanUseSRV (0) is checked for with "AUTODNSSEC"
    capabilities_test.go:86: ok: providers.CanUseSSHFP (0) is checked for with "AUTODNSSEC"
    capabilities_test.go:86: ok: providers.CanUseSVCB (0) is checked for with "AUTODNSSEC"
    capabilities_test.go:86: ok: providers.CanUseTLSA (0) is checked for with "AUTODNSSEC"
--- PASS: TestCapabilitiesAreFiltered (1.69s)
PASS

@philpennock
Copy link
Copy Markdown
Contributor

The second check is to edit pkg/normalize/validate.go to find the providerCapabilityChecks array, and comment out an entry. If the test passes when we're not actually doing the check, then the test has been made bogus.

If I apply this test of the test on main, then the test correctly fails when I comment out DNAME. If I do the same on this branch, then the test passes, which means it has been made ineffective.

@philpennock
Copy link
Copy Markdown
Contributor

See proposed replacement PR in #4195

@TomOnTime
Copy link
Copy Markdown
Collaborator

closing so-as not to confuse with #4195

@TomOnTime TomOnTime closed this May 2, 2026
TomOnTime pushed a commit that referenced this pull request May 8, 2026
…4195)

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.

Co-authored-by: Phil's PT AI Bot Account <phil+ai@pennock-tech.com>
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

Development

Successfully merging this pull request may close these issues.

Replace SA1019: parser.ParseDir

3 participants