Repository navigation
fix(module): set the package ID sub-module only for files in the package - #1038
Conversation
`get_package_id` took the sub-module name by slicing the resolved file name at the length of the package directory, without a check that the file is in that directory. A `types`, `typings`, or `main` field that leaves the package (for example `"types": "../pkg-x/index.d.ts"`) then gave a wrong sub-module name (`x/index.d.ts`), and a multi-byte character at the cut point (`../pkgΓ©/index.d.ts`) panicked. Go `getPackageId` (`resolver.go:2035`) sets `subModuleName` only when `resolvedFileName.RelativeTo(packageDirectory)` succeeds. Port `RootedPath.RelativeTo` (the case-sensitive `trimContainedPath`, `rooted_path.go:575`) as `relative_to_directory` and use it in `get_package_id`. With the wrong sub-module name, two different packages could get the same package ID, so tsrs redirected one file to the other as a duplicate package and dropped a TS2322 that tsgo reports. Tests: the regression cases `package-id-outside-package-dedup` and `package-id-outside-package-utf8`, with expected output from tsgo. Their `node_modules` directories are added with `git add -f`, because the root `.gitignore` ignores `node_modules/`. AI assistance: written with Claude Code.
Merging this PR will not alter performance1 warning
Comparing Footnotes
|
When the
typesfield ofnode_modules/pkg/package.jsonpoints outside the package,get_package_idstill cut the sub-module name at the length of the package directory:"types": "../pkgΓ©/index.d.ts"panicked (start byte index ... is not a char boundary)."types": "../pkg-x/index.d.ts"gave the IDpkg/x/index.d.ts@1.0.0. A file of another package with the same ID was then redirected as a duplicate, and a TS2322 that tsgo reports was missing.Go
getPackageIdsets the sub-module name only whenRelativeTo(packageDirectory)succeeds. This PR portsRelativeTo(GotrimContainedPath) asrelative_to_directory.Tests: the
package-id-outside-package-utf8andpackage-id-outside-package-dedupregressions, with expected output from tsgo. Both fail without the fix.AI assistance: written with Claude Code.