Skip to content

Commit 4a9dcff

Browse files
committed
fix(cli-rs): keep npm cache lookups inside the cache and key specs as npm does
1 parent 2ca3602 commit 4a9dcff

7 files changed

Lines changed: 604 additions & 83 deletions

File tree

‎packages/cli-rs/Cargo.lock‎

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎packages/cli-rs/crates/plugin/Cargo.toml‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ serde.workspace = true
2020
serde_json.workspace = true
2121
unicode-width.workspace = true
2222
opencode-api-types.workspace = true
23+
# The URLs of git specs, parsed as `new URL` parses them (the npm cache's keys, js-npm).
24+
url.workspace = true
2325
# The hash an approval of a project plugin names (rich-plugins 3.9).
2426
ring.workspace = true
2527

‎packages/cli-rs/crates/plugin/src/loader/mod.rs‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1046,7 +1046,11 @@ pub fn discover(directories: &[PathBuf]) -> Vec<PathBuf> {
10461046
pub fn local_source(target: &str, directory: &Path) -> Option<PathBuf> {
10471047
let path = if target.starts_with("file://") {
10481048
opencode_api_types::js::parse_url(target).ok()?.to_file_path().ok()?
1049-
} else if target.starts_with("./") || target.starts_with("../") || Path::new(target).has_root() {
1049+
} else if target.starts_with("./")
1050+
|| target.starts_with("../")
1051+
|| (cfg!(windows) && (target.starts_with(".\\") || target.starts_with("..\\")))
1052+
|| Path::new(target).has_root()
1053+
{
10501054
directory.join(target)
10511055
} else {
10521056
return None;

‎packages/cli-rs/crates/plugin/src/loader/npm.rs‎

Lines changed: 508 additions & 76 deletions
Large diffs are not rendered by default.

‎packages/cli-rs/crates/plugin/src/loader/tests.rs‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -486,6 +486,8 @@ fn local_source_resolves_paths_and_urls_but_not_packages() {
486486
assert_eq!(local_source("/abs/x", dir), Some("/abs/x".into()));
487487
assert_eq!(local_source("file:///abs/y.wasm", dir), Some("/abs/y.wasm".into()));
488488
assert_eq!(local_source("@acme/opencode-tui", dir), None);
489+
// `\` is no separator here: `..\x` names a package, which the cache refuses.
490+
assert_eq!(local_source(r"..\x", dir), None);
489491
// Expected (r2 review, bigboi-ui-8 and carbon-ui-8): a file URL means what `fileURLToPath(new URL(spec))` says.
490492
assert_eq!(local_source("file:///a/My%20Plugin", dir), Some("/a/My Plugin".into()));
491493
assert_eq!(local_source("file://localhost/a/b", dir), Some("/a/b".into()));
@@ -500,6 +502,9 @@ fn local_source_resolves_paths_and_urls_but_not_packages() {
500502
let dir = Path::new(r"C:\Users\me\.config\opencode");
501503
assert_eq!(local_source("./plugins/x", dir), Some(r"C:\Users\me\.config\opencode\plugins\x".into()));
502504
assert_eq!(local_source("../x.wasm", dir), Some(r"C:\Users\me\.config\x.wasm".into()));
505+
// Windows' own relative forms (carbon jsnpm-4), which climb no further than a `/` form does.
506+
assert_eq!(local_source(r".\plugins\x", dir), Some(r"C:\Users\me\.config\opencode\plugins\x".into()));
507+
assert_eq!(local_source(r"..\..\..\x", dir), Some(r"C:\Users\x".into()));
503508
assert_eq!(local_source("/abs/x", dir), Some(r"C:\abs\x".into()));
504509
assert_eq!(local_source(r"\abs\x", dir), Some(r"C:\abs\x".into()));
505510
assert_eq!(local_source(r"D:\abs\x", dir), Some(r"D:\abs\x".into()));

‎packages/cli-rs/crates/tui/tests/all/plugin_npm.rs‎

Lines changed: 74 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,8 @@ fn status(pipe: &Pipe, target: &str) -> Option<Status> {
7373
}
7474

7575
// Expected (TS `Npm.cacheKey`, run with Bun on this checkout): each spec form is keyed in the cache as TS keys it, so
76-
// the same spec finds the same directory: a bare name, a version, ranges, tags, a scope, git and hosted specs, an alias,
77-
// a path, a URL, and specs `npm-package-arg` refuses.
76+
// the same spec finds the same directory: a bare name, a version, ranges, tags, a scope, git and hosted specs (URLs,
77+
// SSH, SCP, shortcuts, committishes), an alias, paths, URLs, tarballs, and specs `npm-package-arg` refuses.
7878
#[test]
7979
fn a_spec_is_keyed_as_ts_keys_it() {
8080
let Some(root) = support::ts_root() else { return };
@@ -113,6 +113,69 @@ fn a_spec_is_keyed_as_ts_keys_it() {
113113
"_foo",
114114
"@s",
115115
"opencode-sidebar-tokens@0.1.3",
116+
// carbon jsnpm-4: hosted git URLs, SSH and SCP, a name with a hosted URL, `~`, and tarballs.
117+
"https://github.com/u/r",
118+
"https://github.com/u/r.git",
119+
"https://gitlab.com/u/r",
120+
"https://bitbucket.org/u/r",
121+
"ssh://git@github.com/u/r.git",
122+
"git@github.com:u/r.git",
123+
"foo@https://github.com/u/r",
124+
"u/r~1",
125+
"plugin.tgz",
126+
"plugin.tar.gz",
127+
"plugin.tar",
128+
// Around them: committishes, other hosts, shortcuts, non-hosted git, paths, and refused specs.
129+
"u/r#semver:^1",
130+
"u/r#a::b",
131+
"u/r#semver:^1::v2",
132+
"github:u/r#abc",
133+
"https://x.org/y.git",
134+
"https://x.org/y",
135+
"git@x.org:y/z.git",
136+
"git+ssh://git@x.org/y.git",
137+
"git+ssh://git@x.org:22/y.git",
138+
"foo@git@github.com:u/r.git",
139+
"foo@u/r",
140+
"./x.tgz",
141+
"/abs/x.tgz",
142+
"x.tgz.bak",
143+
"@s/x@./y.tgz",
144+
"http://github.com/u/r",
145+
"https://github.com/u/r/tree/main",
146+
"https://github.com/u",
147+
"https://www.github.com/u/r",
148+
"github:u",
149+
"gitlab:g/s/p",
150+
"gist:abc123",
151+
"bitbucket:u/r",
152+
"https://gist.github.com/u/abc",
153+
"foo@github:u/r",
154+
"sourcehut:u/r",
155+
"https://git.sr.ht/~u/r",
156+
"~/x",
157+
"~u/r",
158+
"a/b/c",
159+
"u/r.git",
160+
"U/r#x",
161+
"-u/r",
162+
"..",
163+
"../x",
164+
"x/..",
165+
"x/../y",
166+
"@s/../x",
167+
"@s/x/y",
168+
"existing@1.0.0/../../../outside",
169+
"-foo",
170+
"foo bar@",
171+
"node_modules",
172+
"@s/.x",
173+
"@../x",
174+
"ftp://x.org/y",
175+
"git+file:///tmp/r",
176+
"github:u/r%zz",
177+
"https://github.com/u/r%zz",
178+
"foo@npm:bar@1",
116179
];
117180
let script = format!(
118181
"const {{ Npm }} = await import({:?}); const specs = JSON.parse(process.argv[1]); \
@@ -127,9 +190,15 @@ fn a_spec_is_keyed_as_ts_keys_it() {
127190
assert!(output.status.success(), "{output:?}");
128191
let ts: Vec<String> = serde_json::from_slice(&output.stdout).unwrap();
129192
let rust: Vec<String> = specs.iter().map(|spec| opencode_plugin::loader::npm::cache_key(spec)).collect();
130-
for ((spec, ts), rust) in specs.iter().zip(&ts).zip(&rust) {
131-
assert_eq!(rust, ts, "{spec}");
132-
}
193+
let differ: Vec<String> = specs
194+
.iter()
195+
.zip(&ts)
196+
.zip(&rust)
197+
.filter(|((_, ts), rust)| ts != rust)
198+
.map(|((spec, ts), rust)| format!("{spec}: TS {ts}, Rust {rust}"))
199+
.collect();
200+
assert_eq!(ts.len(), specs.len());
201+
assert!(differ.is_empty(), "{} of {} keys differ:\n{}", differ.len(), specs.len(), differ.join("\n"));
133202
}
134203

135204
// Expected (TS `Npm.resolve`, `prepare`): a `cli.json` entry that names an installed package loads its TUI entry from

‎packages/cli-rs/parity/deviations/F16-typescript-plugins-skipped.md‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,15 @@ loader (`crates/plugin/src/loader/npm.rs`) finds the package as `Npm.resolve` do
2525
the key being the spec's text and not a version it resolves to (`npm.ts:60-97`: `<name>@<spec>` for a version, range or
2626
tag, `<name>@latest` for a bare name, `git-<slug>-<hash>` for a git spec, else the spec), in the generation with the
2727
largest number (`npm.ts:149-162`), as `node_modules/<name>` (`installedName`, `npm.ts:173-183`); so the same spec
28-
picks the same directory, and a range is never matched against another version installed under another key. The JS
28+
picks the same directory, and a range is never matched against another version installed under another key. The
29+
classification is `npm-package-arg` 13 and `hosted-git-info` 9 ported: hosted git URLs (HTTPS, SSH, SCP
30+
`git@host:path`, `www.`), `name@<hosted URL>`, GitHub's `user/repo` with a `#committish`, sourcehut's `~user/repo`,
31+
and tarballs keyed by their text. `plugin_npm.rs` checks 95 specs against TS's own `Npm.cacheKey`. Unlike TS, only a
32+
spec TS's `parse` classifies (a version, range or tag of a named package, or a git spec) is looked up (carbon
33+
`jsnpm-4`). A tarball, a URL, an alias or a path keyed by its text names no package, and such a key can lead out of the
34+
cache, as `existing@1.0.0/../../x` does in TS. The name joined under `node_modules` must be a package name (`name`,
35+
`@scope/name`, as `validate-npm-package-name` takes it), so a `dependencies` key that is absolute or `..` is refused.
36+
The package's real path must lie under the cache's `npm` directory, so a link out of it is refused too. The JS
2937
host loads its `./tui` export, as for a directory. The TUI module must be one of the plugin's own files (carbon
3038
`jsnpm-3`). TS's client imports whatever `<name>/tui` resolves to (`packages/plugin/src/host.ts` `resolve`); TS's
3139
server checks only its own server entry (`FSUtil.contains`, `core/src/config/plugin/source.ts`). The JS host refuses a

0 commit comments

Comments
 (0)