Skip to content

Match prefix patterns for dependencies - #28

Merged
owenrumney merged 2 commits into
owenrumney:mainfrom
felix:match-prefix-patterns
Aug 5, 2026
Merged

owenrumney merged 2 commits into
owenrumney:mainfrom
felix:match-prefix-patterns

Conversation

@felix

@felix felix commented Aug 4, 2026

Copy link
Copy Markdown
Contributor

Reasonably naive but stops a lot of false warnings for me.

@owenrumney

Copy link
Copy Markdown
Owner

Thanks for this — the approach is right, but the prefix-only match suppresses more than intended: strings.Cut discards the suffix, so a pattern like %.o has prefix "" and strings.HasPrefix(s, "") matches every dep — one %.o: %.c rule in a Makefile turns off all undefined-target warnings. Same for suffixed patterns like out/%.exe vs a dep out/foo.

Checking both halves fixes it while keeping your src/% case working:

prefix, suffix, hasPattern := strings.Cut(t.Name, "%")
if hasPattern && len(s) > len(prefix)+len(suffix) &&
	strings.HasPrefix(s, prefix) && strings.HasSuffix(s, suffix) {

The length guard mirrors make's rule that % matches at least one character.

Verified locally: with that change, TestDiagnoseFindsPatternDeps still passes, and this regression case goes green:

func TestPatternRuleDoesNotSuppressUnrelatedWarnings(t *testing.T) {
	input := `all: nonexistent
	echo hi

%.o: %.c
	$(CC) -c $< -o $@
`
	mf := parser.Parse(testURI, input)
	diags := Diagnose(mf)

	var found bool
	for _, d := range diags {
		if d.Message == "undefined target: nonexistent" {
			found = true
		}
	}
	assert.True(t, found)
}

Happy to merge with those two changes.

Reasonably naive but stops a lot of false warnings for me.
@felix
felix force-pushed the match-prefix-patterns branch from f7f9606 to bd935ad Compare August 5, 2026 00:52
@owenrumney

Copy link
Copy Markdown
Owner

thanks @felix - appreciate the contribution, releasing now.

@owenrumney
owenrumney merged commit c0a1424 into owenrumney:main Aug 5, 2026
3 checks passed
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.

2 participants