diff --git a/internal/plugin/pi_extensions.go b/internal/plugin/pi_extensions.go index b5468c0eb..38bfdf56b 100644 --- a/internal/plugin/pi_extensions.go +++ b/internal/plugin/pi_extensions.go @@ -445,7 +445,7 @@ func (p *piPackage) evaluate(object, hasRules bool, rules []string) []piRowState if !strings.HasPrefix(rule, "+") && !strings.HasPrefix(rule, "-") { continue } - target := strings.TrimPrefix(rule[1:], "./") + target := piExactPattern(rule[1:], runtime.GOOS) if slices.ContainsFunc(rows, func(r piRowState) bool { return r.rel == target || path.Join(p.abs, r.rel) == target }) { continue } @@ -761,7 +761,7 @@ func piDeltaStates(rules []string, p *piPackage, inherited []piRowState, hasBase if !strings.HasPrefix(rule, "+") && !strings.HasPrefix(rule, "-") { continue } - target := strings.TrimPrefix(rule[1:], "./") + target := piExactPattern(rule[1:], runtime.GOOS) if slices.ContainsFunc(rows, func(r piRowState) bool { return r.rel == target || path.Join(p.abs, r.rel) == target }) { continue } diff --git a/internal/plugin/pi_extensions_match.go b/internal/plugin/pi_extensions_match.go index 71e130916..fc492b44b 100644 --- a/internal/plugin/pi_extensions_match.go +++ b/internal/plugin/pi_extensions_match.go @@ -2,6 +2,8 @@ package plugin import ( "path" + "regexp" + "runtime" "strings" "unicode/utf8" ) @@ -49,10 +51,11 @@ func triAnd(a, b piTri) piTri { // piGlobSupported reports whether a non-exact Pi pattern is in the grammar this // package reproduces: '/'-separated segments of literals with '*' and '?' only, // as minimatch 10 matches them without options (dot files need a literal dot). -// Globstars, braces, classes, extglobs, escapes, comments, negation and '.' or -// '..' segments are left to Pi. The golden test checks this against Pi's minimatch. +// Globstars, braces, classes, extglobs, escapes, comments, negation, paths Pi +// rewrites and '.' or '..' segments are left to Pi. The golden test checks this against +// Pi's minimatch. func piGlobSupported(pattern string) bool { - if pattern == "" || strings.ContainsAny(pattern, "[]{}()|\\") || strings.Contains(pattern, "**") || + if pattern == "" || piRewrittenPattern(pattern, runtime.GOOS) || strings.ContainsAny(pattern, "[]{}()|\\") || strings.Contains(pattern, "**") || strings.HasPrefix(pattern, "#") || strings.HasPrefix(pattern, "!") { return false } @@ -120,10 +123,60 @@ func piGlobMatches(pattern, rel, abs string) piTri { return result } -// piExactMatches is Pi's matchesAnyExactPattern: a plain comparison after -// dropping a leading "./". +// piShellDrive is the Git Bash, MSYS, Cygwin and WSL drive path that Pi's +// normalizeWindowsShellPath turns into a Windows drive path. +var piShellDrive = regexp.MustCompile(`(?i)^/(?:mnt/|cygdrive/)?[a-z](?:/.*)?$`) + +// piRewrittenPattern reports a pattern Pi 1.2.0 and later rewrites with +// normalizePath before matching while older Pi compares it as written: a home +// path, a file:// URL, and on Windows a ~\ home path or a shell drive path. Its +// match depends on the Pi version, so Skillshare leaves it unknown. +func piRewrittenPattern(pattern, goos string) bool { + switch { + case pattern == "~" || strings.HasPrefix(pattern, "~/") || strings.HasPrefix(pattern, "file://"): + return true + case goos != "windows": + return false + } + return strings.HasPrefix(pattern, `~\`) || + !strings.HasPrefix(pattern, "//") && !strings.Contains(pattern, `\`) && piShellDrive.MatchString(pattern) +} + +// piExactState is piExactMatches as a selection: unknown for a pattern Pi +// rewrites after dropping a leading "./" or ".\". +func piExactState(pattern, rel, abs string) piTri { + switch { + case piRewrittenPattern(piDropDot(pattern), runtime.GOOS): + return piUnknown + case piExactMatches(pattern, rel, abs): + return piOn + } + return piOff +} + +// piDropDot drops the leading "./" or ".\" that Pi's normalizeExactPattern drops +// on every platform. +func piDropDot(pattern string) string { + if strings.HasPrefix(pattern, "./") || strings.HasPrefix(pattern, `.\`) { + return pattern[2:] + } + return pattern +} + +// piExactPattern is Pi's normalizeExactPattern before 1.2.0: the leading dot +// dropped, then toPosixPath, which turns "\" into "/" only on Windows. +func piExactPattern(pattern, goos string) string { + pattern = piDropDot(pattern) + if goos == "windows" { + pattern = strings.ReplaceAll(pattern, `\`, "/") + } + return pattern +} + +// piExactMatches is Pi's matchesAnyExactPattern: a plain comparison with the +// normalized pattern. func piExactMatches(pattern, rel, abs string) bool { - pattern = strings.TrimPrefix(pattern, "./") + pattern = piExactPattern(pattern, runtime.GOOS) return pattern == rel || pattern == abs } @@ -135,7 +188,7 @@ func piExactRule(rule, rel, abs string) bool { type piEvaluation struct { state piTri - exact string // the last exact rule that decided the file, if any + exact string // the last exact rule that decided the file, or a rewritten rule that may globs []string // the include and ! rules that match it, or may } @@ -146,15 +199,22 @@ func piEvaluate(rules []string, rel, abs string) piEvaluation { included, hasInclude := piOff, false excluded := piOff forceOn, forceOff := false, false + rewrittenOn, rewrittenOff := "", "" // the last rewritten rule that may force the file for _, rule := range rules { switch { case strings.HasPrefix(rule, "+"): - if piExactMatches(rule[1:], rel, abs) { + switch piExactState(rule[1:], rel, abs) { + case piOn: forceOn, e.exact = true, rule + case piUnknown: + rewrittenOn = rule } case strings.HasPrefix(rule, "-"): - if piExactMatches(rule[1:], rel, abs) { + switch piExactState(rule[1:], rel, abs) { + case piOn: forceOff, e.exact = true, rule + case piUnknown: + rewrittenOff = rule } case strings.HasPrefix(rule, "!"): if m := piGlobMatches(rule[1:], rel, abs); m != piOff { @@ -179,6 +239,8 @@ func piEvaluate(rules []string, rel, abs string) piEvaluation { e.state = triAnd(included, triNot(excluded)) if forceOn { e.state = piOn + } else if rewrittenOn != "" && e.state != piOn { + e.state, e.exact = piUnknown, rewrittenOn } if forceOff { e.state = piOff @@ -188,6 +250,8 @@ func piEvaluate(rules []string, rel, abs string) piEvaluation { e.exact = rule } } + } else if rewrittenOff != "" && e.state != piOff { + e.state, e.exact = piUnknown, rewrittenOff } return e } @@ -208,10 +272,7 @@ func piEvaluateDelta(rules []string, rel, abs string) (state piTri, named bool, } var m piTri if exact { - m = piOff - if piExactMatches(target, rel, abs) { - m = piOn - } + m = piExactState(target, rel, abs) } else { m = piGlobMatches(target, rel, abs) } diff --git a/internal/plugin/pi_extensions_match_test.go b/internal/plugin/pi_extensions_match_test.go index c4b0e73af..a39d1e394 100644 --- a/internal/plugin/pi_extensions_match_test.go +++ b/internal/plugin/pi_extensions_match_test.go @@ -77,6 +77,17 @@ func TestPiEvaluateFollowsPiPatternOrder(t *testing.T) { {"+ beats !, - beats +", []string{"!extensions/*.ts", "+extensions/a.ts", "+extensions/b.ts", "-extensions/b.ts"}, []piTri{piOn, piOff, piOff}}, {"unsupported glob is unknown unless an exact rule decides", []string{"!extensions/**", "+extensions/a.ts", "-extensions/b.ts"}, []piTri{piOn, piOff, piUnknown}}, {"./ prefix on exact rules", []string{"-./extensions/c.ts"}, []piTri{piOn, piOn, piOff}}, + {".\\ prefix on exact rules", []string{`-.\extensions/c.ts`}, []piTri{piOn, piOn, piOff}}, + // Pi 1.2.0 rewrites these rules with normalizePath; older Pi compares them as written. + {"~ exact exclude is unknown", []string{"-~/pkg/extensions/a.ts"}, []piTri{piUnknown, piUnknown, piUnknown}}, + {"~ exact include is unknown unless already on", []string{"!extensions/a.ts", "+~/pkg/extensions/a.ts"}, []piTri{piUnknown, piOn, piOn}}, + {"~ glob exclude is unknown", []string{"!~/pkg/extensions/*.ts"}, []piTri{piUnknown, piUnknown, piUnknown}}, + {"a known exact exclude still decides over ~", []string{"-~/pkg/extensions/a.ts", "-extensions/a.ts"}, []piTri{piOff, piUnknown, piUnknown}}, + {"bare ~ is a home path", []string{"-~"}, []piTri{piUnknown, piUnknown, piUnknown}}, + {"./~ is a home path", []string{"-./~/pkg/extensions/a.ts"}, []piTri{piUnknown, piUnknown, piUnknown}}, + {"~name stays literal", []string{"-~a.ts"}, []piTri{piOn, piOn, piOn}}, + {"~ include glob is unknown", []string{"~/pkg/extensions/*.ts"}, []piTri{piUnknown, piUnknown, piUnknown}}, + {"file:// exact is unknown", []string{"-file:///pkg/extensions/a.ts"}, []piTri{piUnknown, piUnknown, piUnknown}}, } for _, c := range cases { t.Run(c.name, func(t *testing.T) { @@ -88,3 +99,57 @@ func TestPiEvaluateFollowsPiPatternOrder(t *testing.T) { }) } } + +func TestPiEvaluateDeltaLeavesHomeRulesUnknown(t *testing.T) { + if state, named, _ := piEvaluateDelta([]string{"-~/pkg/extensions/a.ts"}, "extensions/a.ts", "/pkg/extensions/a.ts"); state != piUnknown || !named { + t.Errorf("got %v named=%v, want unknown and named", state, named) + } +} + +func TestPiEvaluateNamesTheHomeRuleItLeavesUnknown(t *testing.T) { + if got := piEvaluate([]string{"-~/pkg/extensions/a.ts"}, "extensions/a.ts", "/pkg/extensions/a.ts").exact; got != "-~/pkg/extensions/a.ts" { + t.Errorf("exact = %q, want the home rule", got) + } +} + +func TestPiRewrittenPatternFollowsPiNormalizePath(t *testing.T) { + cases := []struct { + pattern, goos string + want bool + }{ + {"~", "linux", true}, + {"~/x.ts", "linux", true}, + {"file:///pkg/x.ts", "linux", true}, + {`~\x.ts`, "linux", false}, + {"/c/Users/me/x.ts", "linux", false}, + {`~\x.ts`, "windows", true}, + {"/c/Users/me/x.ts", "windows", true}, + {"/mnt/d/x.ts", "windows", true}, + {"/cygdrive/C", "windows", true}, + {"//server/share/x.ts", "windows", false}, + {"/pkg/x.ts", "windows", false}, + {"~x.ts", "windows", false}, + {"extensions/x.ts", "windows", false}, + } + for _, c := range cases { + if got := piRewrittenPattern(c.pattern, c.goos); got != c.want { + t.Errorf("piRewrittenPattern(%q, %s) = %v, want %v", c.pattern, c.goos, got, c.want) + } + } +} + +func TestPiExactPatternFollowsPiNormalizeExactPattern(t *testing.T) { + cases := []struct{ pattern, goos, want string }{ + {"./extensions/a.ts", "linux", "extensions/a.ts"}, + {`.\extensions/a.ts`, "linux", "extensions/a.ts"}, + {`extensions\a.ts`, "linux", `extensions\a.ts`}, + {`.\extensions\a.ts`, "windows", "extensions/a.ts"}, + {`C:\pkg\extensions\a.ts`, "windows", "C:/pkg/extensions/a.ts"}, + {"././a.ts", "windows", "./a.ts"}, + } + for _, c := range cases { + if got := piExactPattern(c.pattern, c.goos); got != c.want { + t.Errorf("piExactPattern(%q, %s) = %q, want %q", c.pattern, c.goos, got, c.want) + } + } +} diff --git a/ui/src/components/targets/TargetPiExtensions.test.tsx b/ui/src/components/targets/TargetPiExtensions.test.tsx index d1c1fcb76..d5cb112c1 100644 --- a/ui/src/components/targets/TargetPiExtensions.test.tsx +++ b/ui/src/components/targets/TargetPiExtensions.test.tsx @@ -194,4 +194,12 @@ describe('Pi target Extensions tab', () => { expect(await screen.findByRole('status')).toHaveTextContent('Pi uses it only if it trusts this project'); }); + it('names the exact rule Skillshare can not evaluate', async () => { + const base = global().packages[0]; + show(global({ packages: [{ ...base, rules: ['-~/pkgs/tools/extensions/a.ts'], rows: [ + { path: 'extensions/a.ts', file: 'present', selection: 'unknown', origin: 'rule', rule: '-~/pkgs/tools/extensions/a.ts', editable: false }, + ] }] })); + expect(await screen.findByText("Decided by a pattern Skillshare can't evaluate: -~/pkgs/tools/extensions/a.ts. Change it with pi config.")).toBeInTheDocument(); + }); + }); diff --git a/ui/src/components/targets/TargetPiExtensions.tsx b/ui/src/components/targets/TargetPiExtensions.tsx index c4e93fc79..5022ced77 100644 --- a/ui/src/components/targets/TargetPiExtensions.tsx +++ b/ui/src/components/targets/TargetPiExtensions.tsx @@ -333,7 +333,8 @@ function RowOrigin({ row, action, canRemove, onDefault, onKeep, t }: { row: PiEx if (action) return {t('targetDetail.piExtensions.origin.pendingRule', { rule: `${action === 'select' ? '+' : '-'}${row.path}` })}; // Skillshare can't say what an undecidable rule selects; it says why and where to change it. if (row.selection === 'unknown') { - return {row.globs?.length ? t('targetDetail.piExtensions.unknown.glob', { globs: row.globs.join(' ') }) : t('targetDetail.piExtensions.unknown.rule')}; + const undecided = row.globs?.length ? row.globs.join(' ') : row.rule; + return {undecided ? t('targetDetail.piExtensions.unknown.glob', { globs: undecided }) : t('targetDetail.piExtensions.unknown.rule')}; } return ( <>