Repository navigation
fix(resolution): an undeclared package that is installed is the package's (#3271) - #3278
Open
inth3shadows wants to merge 5 commits into
Open
inth3shadows wants to merge 5 commits into
inth3shadows wants to merge 5 commits into
Conversation
…ge's (colbymchenry#3271) A bare JS/TS import of a package the importing file's package.json (and its enclosing ones) doesn't declare counted as the project's, so a name it binds fell through to a project-wide exact match: `import { useForm } from 'react-hook-form'` linked to an unrelated `useForm` the file never imports. isOutOfRepoImport now also treats such a specifier as outside the repository when Node would find it installed: the nearest `node_modules/<name>` at or above the importer's real path (past the project root too) whose real path runs through a `node_modules` folder (a registry copy or pnpm's store). A `node_modules` entry linking to any other folder is a sibling package and stays the project's, as do aliases the resolver doesn't read (a bundler's `ui`, an import map's `helpers`), which have no installed copy. Known limits: a checkout with nothing installed keeps today's behaviour; an alias that shares its name with an installed package now counts as that package.
An install can copy a sibling package into node_modules as a real folder (yarn v1's `file:`, npm's install-links), and a project can itself live inside a node_modules folder. Both looked like registry copies, so an undeclared import of the sibling lost its edges. A name that a package.json of an indexed directory owns now stays the project's; the set is read only when a name would otherwise count as installed. Also: memoize each node_modules folder's real path, and bring the isInstalledPackage and importedFromPackage comments in line with the rule.
…sion matches The previous commit kept any installed name that some project manifest owns, so a fixture or tool folder named like a real dependency (or a `file:` tarball dependency) brought the colbymchenry#3271 mislink back. An installed copy now stays the project's only when a package.json at or above an indexed directory gives it the same name and version, which a copied sibling or a project inside node_modules does and a namesake fixture doesn't. The manifest read is one package.json per directory, built only when a name would otherwise count as installed, and the answer is memoized per node_modules folder and name. The node_modules walk now lists each folder only when it gets to it, so a nearer answer leaves the rest unread.
…rsion Matching an installed copy to a project package by name and version dropped correct edges: a copy left behind by a version bump, a copy installed under another name (`"kit": "file:../libs/lib"`), and a package.json that starts with a byte order mark. An installed copy is the project's again when a project manifest names it, either as the name it is imported by or as the name its own package.json gives, read past a byte order mark. Manifests are read directly, so the sweep doesn't evict cached source text.
The two package.json reads added for copied siblings opened the file with a plain read, so a FIFO (or a link to a device) committed at node_modules/<name>/package.json blocked indexing forever. They now go through readSourceText, which opens without blocking and takes only a regular file within the source size limit, and strip a byte order mark. npm's bare local-path dependency specs (`"lib": "../libs/lib"`) now count as the project's like `file:` ones, so a nameless sibling copied in that way keeps its edges.
inth3shadows
added a commit
to inth3shadows/codegraph
that referenced
this pull request
Nov 12, 2026
This was referenced Nov 12, 2026
Merged
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #3271
What
A bare JS/TS import of a package that the importing file's
package.json(and the ones above it) doesn't declare counted as the project's. Soimport { useForm } from 'react-hook-form'with the dependency hoisted to a monorepo root fell through to a project-wide exact match and linked to an unrelateduseFormthe file never imports.How
isOutOfRepoImportnow also treats such a specifier as outside the repository when Node would find it installed. That means the nearestnode_modules/<name>at or above the importer's real path, past the project root too, whose real path runs through anode_modulesfolder: a registry copy, or pnpm's.pnpmstore. Names are matched against a case-exact listing of eachnode_modulesfolder.These stay the project's, as on
main:node_modulesentry that links to any other folder, which is a sibling package. That covers workspace links, a worktree whosenode_moduleslinks to the main checkout, and apackages/folder linked out of the project.package.jsonat or above an indexed folder names it, either as itsnameor as aworkspace:/file:/link:/portal:or bare local-path dependency, under the name it's imported by or the name the copy's ownpackage.jsongives. That covers a sibling that yarn'sfile:or npm's install-links copied in (stale or under another name), and a project that itself lives inside anode_modulesfolder.ui, a Deno import map'shelpers, a tsconfigpathsin a referenced config) or$lib/…,~/…,@/…and#….I tried two other designs first and dropped both:
Measured (base
f72c9d2evs this branch,CODEGRAPH_KERNEL=0)__tests__/undeclared-package-imports.test.ts: 35/35 pass. Onmain, the 11 wrong-edge cases fail and the 24 kept cases pass.mainwith nothing installed, and about 5% slower with all 15 installed (4.08–4.37s against 3.88–4.24s on a loaded machine). Edges are identical in both cases.tsc --noEmitexits 0.package.jsonfiles are read through the same guard as source files (no blocking open, regular files only, size limit). A FIFO committed atnode_modules/<name>/package.jsondoesn't stall indexing; a test pins this on POSIX./var→/private/var, Windows junctions) aren't run here; the tests create links asjunctionso they can run on Windows.Checked with graph-assay
I maintain graph-assay, which checks a code graph's edges against the compiler. I added a fixture for this issue:
fixtures/oracle-candidates/issue-3271, withreact-hook-forminstalled in the fixture.issue-3271is BROKEN onmain(f72c9d2e) and FIXED on this PR (381f34db). No other fixture verdict changed.dd2f8fcwith its yarn-installed dependencies, the TypeScript compiler oracle sees two edges change, both removals:generateElementCanvas -> AppCursor::canvasatpackages/element/src/renderElement.ts:325(rough.canvas(canvas)). The compiler resolves this call toroughjs, so the edge was wrong.woff2ServerPlugin -> whichatscripts/woff2/woff2-esbuild-plugins.js:103, an edge to the localconst which = require("which")binding. The compiler counts that edge as correct.mainalready drops the same edge whenwhichis declared inpackage.json, so this PR makes an undeclared but installed package behave the same way.To rerun, from a graph-assay checkout at that commit:
Known limits
package.jsonrepro whennode_modulesis absent.package.jsonis named like an installed dependency (a test fixture namedreact-hook-form) makes that dependency count as the project's, so its imports keep today's behaviour.uiwhile a packageuiis installed) now counts as that package, which is what Node would load without the bundler.node_modulesthat is itself a link to a folder not namednode_modules, a symlinked source file, annpm linked checkout elsewhere, a package with only@types/<name>installed (such asvscode), and a package installed as a single file..,.., or a Windows path (..\libs\lib,C:\libs\lib) isn't recognized as the project's, so it counts as installed.pathsentry ("*": ["./src/*"]) that resolves an undeclared installed package to a project folder keeps today's behaviour; the check here runs only for imports no project file answers.file:tarball or other local dependency in the importer's own manifest makes that name count as the project's, as it does today.package.jsonhas no name (or a different one), declared only from a folder with no indexed source, counts as installed.node_moduleschanges. The same is true ofpackage.jsontoday, so the CHANGELOG entry asks for a re-index.