Repository navigation
Resolve package self-imports, directory imports, and values over same-named types #727
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
jaywyawhare
wants to merge
1
commit into
hydra-db:bench/code-corpus
Choose a base branch
from
jaywyawhare:fix/resolve-self-imports
base: bench/code-corpus
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+193
−22
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| Sources for the self-import and directory-import tests in `tests/test_code_resolve.py` and | ||
| `typescript/test/codeResolve.test.ts`, and for the conformance case that compares the graphs both SDKs | ||
| build from them. `pkg` is a package root that imports itself by name; `web` imports folders by their index. | ||
| Nothing here is run or built. |
Empty file.
Empty file.
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| class App: | ||
| def run(self): | ||
| return 1 | ||
|
|
||
|
|
||
| def helper(): | ||
| return 2 |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| import pkg.cli.app | ||
| from pkg.cli.app import App | ||
|
|
||
|
|
||
| def go(): | ||
| App() | ||
| return pkg.cli.app.helper() |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| export interface Ok { | ||
| v: number; | ||
| } | ||
|
|
||
| export function Ok(): Ok { | ||
| return { v: 1 }; | ||
| } |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| export function hello(): number { | ||
| return 1; | ||
| } |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| export function local(): number { | ||
| return 2; | ||
| } |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| import { hello } from ".."; | ||
| import { local } from "."; | ||
| import * as core from "../core"; | ||
|
|
||
| export function go(): number { | ||
| return hello() + local() + core.Ok().v; | ||
| } |
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,4 @@ | ||
| import { basename, normalize } from "node:path"; | ||
| import { comparePy, cpLen, isPlainObject, PY_WS_CLASS, pyStrip } from "../internal/py.js"; | ||
| import { type ModuleSpec, type ResolveSpec, tables } from "./languages.js"; | ||
| import type { Binding, Declaration, FileModel, ProjectModel, Request, ResolvedEdge } from "./model.js"; | ||
|
|
@@ -208,6 +209,22 @@ function fits(request: Request, method: string, template: string[]): boolean { | |
| return template.every((t, i) => t === segs[i] || (segs[i] !== "" && SLOT.test(t))); | ||
| } | ||
|
|
||
| /** | ||
| * The declaration a scope binds under a name: the type in a type position, else the value, so a function or | ||
| * constant wins over an interface or type alias of the same name. | ||
| * | ||
| * @param entries (Entry[]): what the scope binds under the name. | ||
| * @param types (boolean): whether the name is in a type position. | ||
| * @return (Sym | null): the declaration, or null. | ||
| */ | ||
| function pick(entries: Entry[], types: boolean): Sym | null { | ||
| const syms = entries.filter((entry) => entry[0] === "sym").map(([, value]) => value as Sym); | ||
| const wanted = syms.find((sym) => | ||
| types ? TYPE_KINDS.has(sym.kind) : sym.kind !== "interface" && sym.kind !== "type", | ||
| ); | ||
| return wanted ?? syms[0] ?? null; | ||
| } | ||
|
|
||
| class Sym { | ||
| readonly id: string; | ||
| readonly kind: string; | ||
|
|
@@ -503,7 +520,8 @@ class Resolver { | |
| while (scope !== null) { | ||
| const hidden = scope !== start && scope.decl !== null && TYPE_KINDS.has(scope.decl.kind); | ||
| const entries = hidden && f.r.private_class_scope ? [] : (scope.names.get(name) ?? []); | ||
| for (const [kind, value] of entries) if (kind === "sym") return ["sym", value, "scope"]; | ||
| const sym = pick(entries, types); | ||
| if (sym !== null) return ["sym", sym, "scope"]; | ||
| for (const [kind, value] of entries) { | ||
| if (kind !== "import") continue; | ||
| const found = this.importOf(f, value); | ||
|
|
@@ -675,7 +693,8 @@ class Resolver { | |
| */ | ||
| top(m: File, name: string): Value | null { | ||
| const entries = m.top.names.get(name) ?? []; | ||
| for (const [kind, value] of entries) if (kind === "sym") return ["sym", value]; | ||
| const sym = pick(entries, false); | ||
| if (sym !== null) return ["sym", sym]; | ||
| for (const [kind, value] of entries) if (kind === "import") return this.importOf(m, value); | ||
| const held = entries.filter(([kind]) => kind === "var").map(([, value]) => value as Binding); | ||
| if (held.length) return ["var", [m, held]]; | ||
|
|
@@ -718,7 +737,7 @@ class Resolver { | |
| } | ||
| bases = [[f.root, parts.slice(0, parts.length - dots + 1).join("/")]]; | ||
| path = text.slice(dots).split(sep).join("/"); | ||
| } else if (relative || text.startsWith("./") || text.startsWith("../")) { | ||
| } else if (relative || text.startsWith("./") || text.startsWith("../") || text === "." || text === "..") { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same fix needed here for |
||
| bases = [[f.root, f.dir]]; | ||
| } else if (Object.keys(m).length) { | ||
| let segs = sep !== "/" ? text.split(sep).filter((s) => s) : [text]; | ||
|
|
@@ -745,6 +764,7 @@ class Resolver { | |
| const candidates: [number, string][] = [ | ||
| ...bases.map(([root, base]): [number, string] => [root, norm(`${base}/${path}`)]), | ||
| ...this.configured(f, text), | ||
| ...this.own(f, text), | ||
| ]; | ||
| let out: File[] = []; | ||
| for (const [root, full] of candidates) { | ||
|
|
@@ -753,7 +773,7 @@ class Resolver { | |
| out = this.files.filter((o) => o.root === root && o.dir === full && o.r === f.r); | ||
| } else { | ||
| const suffixes = m.suffixes ?? [""]; | ||
| let names = suffixes.map((s) => full + s); | ||
| let names = suffixes.map((s) => (full + s).replace(/^[/]/, "")); | ||
| if (m.compiled && new RegExp(m.compiled).test(full)) { | ||
| const base = full.replace(new RegExp(m.compiled), ""); | ||
| names = [...suffixes.filter((s) => s && !s.includes("/")).map((s) => base + s), ...names]; | ||
|
|
@@ -769,6 +789,26 @@ class Resolver { | |
| return out; | ||
| } | ||
|
|
||
| /** | ||
| * The paths a module name maps to when its first segment names a root that is itself a package. | ||
| * | ||
| * @param f (File): the file. | ||
| * @param text (string): the module name. | ||
| * @return ([number, string][]): (root index, path) pairs to try. | ||
| */ | ||
| own(f: File, text: string): [number, string][] { | ||
| const sep = f.m.separator ?? "/"; | ||
| const [head, , rest] = partition(text, sep); | ||
| const out: [number, string][] = []; | ||
| this.roots.forEach((root, index) => { | ||
| if (head !== basename(normalize(root))) return; | ||
| if ((f.m.packages ?? []).some((marker) => this.byPath.has(`${index}\0${marker}`))) { | ||
| out.push([index, rest.split(sep).join("/")]); | ||
| } | ||
| }); | ||
| return out; | ||
| } | ||
|
|
||
| /** | ||
| * The directory a leading path segment such as crate, self, or super stands for. | ||
| * | ||
|
|
@@ -867,7 +907,11 @@ class Resolver { | |
| result = cls !== null ? [cls, "member"] : null; | ||
| } else { | ||
| const found = this.path(f, offset, segs, true); | ||
| if (found !== null && found[0][0] === "sym" && TYPE_KINDS.has(found[0][1].kind)) result = [found[0][1], found[1]]; | ||
| let sym = found !== null && found[0][0] === "sym" ? found[0][1] : null; | ||
| if (sym !== null && sym.parent === null && !TYPE_KINDS.has(sym.kind)) { | ||
| sym = pick(sym.file.top.names.get(sym.name) ?? [], true); | ||
| } | ||
| if (found !== null && sym !== null && TYPE_KINDS.has(sym.kind)) result = [sym, found[1]]; | ||
| } | ||
| this.active.delete(key); | ||
| this.cache.set(key, result); | ||
|
|
||
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
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
Oops, something went wrong.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A bare
"."(and"..", same code) becomes the folder path and then the file suffixes are tried before/index.*, so a sibling file named after the folder wins.Repro:
web/sub.tsandweb/sub/index.tsboth exportwrong;web/sub/use.tshasimport { wrong } from "."; wrong(). Both SDKs giveCALLS function:web/sub/use.ts:go -> function:web/sub.ts:wrong.ts.resolveModuleNameresolves it toweb/sub/index.tsunder Node10, Node16 and Bundler.For
.and..only try the/index.*suffixes. Please add the sibling file to thecode-selffixture so the test covers it.