@dimassetoid/septum-mcp 0.2.1 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +46 -28
- package/package.json +4 -4
- package/src/cli/commands/check.ts +27 -16
- package/src/cli/commands/hook.ts +38 -101
- package/src/cli/commands/init.ts +26 -9
- package/src/cli/commands/sync.ts +4 -6
- package/src/cli/hook-payload.ts +43 -0
- package/src/cli/index.ts +9 -52
- package/src/cli/scaffold/harness-config.ts +48 -8
- package/src/cli/templates/hooks.ts +49 -14
- package/src/cli/templates/rules.ts +34 -49
- package/src/cli/templates/skill.ts +39 -35
- package/src/core/boundary/evaluator.ts +67 -83
- package/src/core/boundary/staged-auditor.ts +102 -40
- package/src/core/config/domain-ownership.ts +45 -0
- package/src/core/config/loader.ts +81 -29
- package/src/core/config/schema.ts +0 -10
- package/src/core/database/repositories/dependency.repository.ts +169 -148
- package/src/core/database/repositories/domain.repository.ts +46 -3
- package/src/core/database/repositories/symbol.repository.ts +226 -87
- package/src/core/database/repository.ts +73 -18
- package/src/core/database/schema.ts +2 -33
- package/src/core/discovery/navigation.ts +105 -0
- package/src/core/discovery/topology-detector.ts +60 -2
- package/src/core/ingestion/pipeline.ts +96 -129
- package/src/core/parser/behavioral-analyzer.ts +26 -22
- package/src/core/parser/boundary-tracker.ts +15 -0
- package/src/core/parser/extractors/base.ts +7 -3
- package/src/core/parser/extractors/csharp.ts +7 -3
- package/src/core/parser/extractors/django-semantic.ts +4 -36
- package/src/core/parser/extractors/elysia-semantic.ts +3 -24
- package/src/core/parser/extractors/express-semantic.ts +3 -18
- package/src/core/parser/extractors/fastapi-semantic.ts +19 -53
- package/src/core/parser/extractors/fastify-semantic.ts +3 -18
- package/src/core/parser/extractors/flask-semantic.ts +22 -55
- package/src/core/parser/extractors/go-semantic.ts +92 -33
- package/src/core/parser/extractors/hono-semantic.ts +3 -24
- package/src/core/parser/extractors/java.ts +5 -3
- package/src/core/parser/extractors/laravel-semantic.ts +44 -16
- package/src/core/parser/extractors/mcp-cli-semantic.ts +6 -7
- package/src/core/parser/extractors/nestjs-semantic.ts +3 -19
- package/src/core/parser/extractors/php.ts +16 -0
- package/src/core/parser/extractors/python.ts +9 -4
- package/src/core/parser/extractors/registry.ts +26 -5
- package/src/core/parser/extractors/ruby.ts +8 -5
- package/src/core/parser/extractors/rust.ts +31 -13
- package/src/core/parser/extractors/semantic-extractor.interface.ts +3 -1
- package/src/core/parser/extractors/symfony-semantic.ts +4 -37
- package/src/core/parser/extractors/typescript.ts +14 -1
- package/src/core/parser/implementation-fingerprint.ts +97 -0
- package/src/core/parser/language-capabilities.ts +23 -7
- package/src/core/parser/schema/providers/laravel-migration-provider.ts +73 -50
- package/src/core/parser/schema/schema-provider-registry.ts +1 -3
- package/src/core/parser/schema/schema-provider.interface.ts +2 -1
- package/src/core/parser/semantic-revision.ts +3 -0
- package/src/core/parser/static-string.ts +51 -0
- package/src/core/resolver/call-graph-tracer.ts +337 -28
- package/src/core/resolver/import-resolver.ts +107 -179
- package/src/core/resolver/interpretation-inputs.ts +7 -0
- package/src/core/resolver/languages/go.ts +103 -0
- package/src/core/resolver/languages/managed.ts +200 -0
- package/src/core/resolver/languages/python.ts +115 -0
- package/src/core/resolver/languages/ruby.ts +40 -0
- package/src/core/resolver/languages/rust.ts +127 -0
- package/src/core/resolver/languages/shared.ts +71 -0
- package/src/core/resolver/module-resolver.ts +8 -4
- package/src/core/resolver/slice-catalog.ts +79 -0
- package/src/core/resolver/slice-format.ts +9 -0
- package/src/core/resolver/symbol-impact.ts +381 -0
- package/src/core/resolver/vertical-slice-tracer.ts +93 -115
- package/src/core/resources/ignore.ts +22 -1
- package/src/core/resources/source-files.ts +143 -20
- package/src/core/resources/source-inventory.ts +55 -0
- package/src/core/retrieval/context-page.ts +109 -0
- package/src/core/retrieval/context.ts +320 -0
- package/src/core/retrieval/resolution-warnings.ts +21 -0
- package/src/core/retrieval/symbol-evidence.ts +97 -0
- package/src/core/retrieval/typescript-references.ts +262 -0
- package/src/core/telemetry/telemetry.ts +54 -9
- package/src/index.ts +0 -3
- package/src/mcp/schemas.ts +61 -420
- package/src/mcp/server.ts +116 -290
- package/src/mcp/tools/boundary.ts +2 -2
- package/src/mcp/tools/check-boundary.ts +55 -60
- package/src/mcp/tools/get-domain-catalog.ts +32 -219
- package/src/mcp/tools/get-symbol-impact.ts +45 -5
- package/src/mcp/tools/get-symbol.ts +227 -3
- package/src/mcp/tools/locate-symbol.ts +54 -14
- package/src/mcp/tools/{domain.ts → map.ts} +4 -18
- package/src/mcp/tools/register-domain.ts +41 -3
- package/src/mcp/tools/symbol.ts +23 -21
- package/src/mcp/tools/trace-vertical-slice.ts +36 -10
- package/src/mcp/workspace-manager.ts +15 -4
- package/src/mcp/workspace-watcher.ts +8 -8
- package/src/types/index.ts +35 -53
- package/src/cli/commands/topology.ts +0 -161
- package/src/core/database/repositories/environment.repository.ts +0 -207
- package/src/core/parser/schema/providers/prisma-schema-provider.ts +0 -90
- package/src/core/session/session-manager.ts +0 -107
- package/src/core/topology/environment-detector.ts +0 -973
- package/src/core/topology/types.ts +0 -83
- package/src/mcp/tools/feature.ts +0 -67
- package/src/mcp/tools/get-environment-topology.ts +0 -138
- package/src/mcp/tools/get-feature-context.ts +0 -224
- package/src/mcp/tools/get-symbol-hotspots.ts +0 -54
- package/src/mcp/tools/register-feature.ts +0 -110
|
@@ -1,25 +1,21 @@
|
|
|
1
1
|
import * as fs from "node:fs";
|
|
2
|
+
import { resolvePython } from "./languages/python.ts";
|
|
3
|
+
import { resolveGo } from "./languages/go.ts";
|
|
4
|
+
import { resolveRuby } from "./languages/ruby.ts";
|
|
5
|
+
import { resolveRust } from "./languages/rust.ts";
|
|
6
|
+
import { resolveJava, resolveCSharp } from "./languages/managed.ts";
|
|
7
|
+
import type { ResolutionIO } from "./languages/shared.ts";
|
|
2
8
|
import * as path from "node:path";
|
|
3
9
|
import { builtinModules } from "node:module";
|
|
4
10
|
import { computeContentHash } from "../ingestion/hasher.ts";
|
|
5
|
-
import { readSourceFile } from "../resources/source-files.ts";
|
|
11
|
+
import { readSourceFile, probeSourcePath, readSourceDirectory } from "../resources/source-files.ts";
|
|
6
12
|
import type { ImportResolution, ResolutionTarget } from "../../types/index.ts";
|
|
7
13
|
|
|
8
|
-
export const RESOLVER_REVISION = "
|
|
14
|
+
export const RESOLVER_REVISION = "5";
|
|
9
15
|
const GO_STDLIB = new Set("archive bufio builtin bytes cmp compress container context crypto database debug embed encoding errors expvar flag fmt go hash html image index io iter log maps math mime net os path plugin reflect regexp runtime slices sort strconv strings structs sync syscall testing text time unicode unique unsafe".split(" "));
|
|
10
16
|
|
|
11
17
|
const PYTHON_STDLIB = new Set("abc argparse array ast asyncio atexit base64 bdb binascii bisect builtins bz2 calendar cgi cgitb chunk cmath cmd code codecs codeop collections colorsys compileall concurrent configparser contextlib contextvars copy copyreg cProfile crypt csv ctypes curses dataclasses datetime dbm decimal difflib dis distutils doctest email encodings ensurepip enum errno faulthandler fcntl filecmp fileinput fnmatch fractions ftplib functools gc getopt getpass gettext glob graphlib gzip hashlib heapq hmac html http idlelib imaplib imghdr imp importlib inspect io ipaddress itertools json keyword lib2to3 linecache locale logging lzma mailbox mailcap marshal math mimetypes mmap modulefinder msilib msvcrt multiprocessing netrc nntplib numbers operator optparse os ossaudiodev pathlib pdb pickle pickletools pipes pkgutil platform plistlib poplib posix posixpath pprint profile pstats pty pwd py_compile pyclbr pydoc queue quopri random re readline reprlib resource rlcompleter runpy sched secrets select selectors shelve shlex shutil signal site smtpd smtplib sndhdr socket socketserver spwd sqlite3 ssl stat statistics string stringprep struct subprocess sunau symbol symtable sys sysconfig syslog tabnanny tarfile telnetlib tempfile termios test textwrap threading time timeit tkinter token tokenize tomllib trace traceback tracemalloc tty turtle turtledemo types typing unicodedata unittest urllib uu uuid venv warnings wave weakref webbrowser winreg winsound wsgiref xdrlib xml xmlrpc zipapp zipfile zipimport zlib zoneinfo".split(" "));
|
|
12
18
|
|
|
13
|
-
const PYTHON_DIST_TO_MODULE_MAP: Record<string, string[]> = {
|
|
14
|
-
pillow: ["pil"],
|
|
15
|
-
pyyaml: ["yaml"],
|
|
16
|
-
python_dotenv: ["dotenv"],
|
|
17
|
-
scikit_learn: ["sklearn"],
|
|
18
|
-
beautifulsoup4: ["bs4"],
|
|
19
|
-
opencv_python: ["cv2"],
|
|
20
|
-
protobuf: ["google"],
|
|
21
|
-
typing_extensions: ["typing_extensions"],
|
|
22
|
-
};
|
|
23
19
|
|
|
24
20
|
const PHP_STDLIB = new Set([
|
|
25
21
|
"stdClass", "Exception", "ErrorException", "Error", "TypeError", "ValueError", "ArgumentCountError",
|
|
@@ -41,8 +37,10 @@ export class ImportResolver {
|
|
|
41
37
|
private root: string;
|
|
42
38
|
constructor(root: string, private packageRoots: string[] = []) { this.root = path.resolve(root); }
|
|
43
39
|
|
|
44
|
-
public resolve(target: string, sourceFile: string): ImportResolution {
|
|
40
|
+
public resolve(target: string, sourceFile: string, statement?: string): ImportResolution {
|
|
45
41
|
const manifests: Record<string, string> = {};
|
|
42
|
+
const contents = new Map<string, string | undefined>();
|
|
43
|
+
let bytesRead = 0;
|
|
46
44
|
const result: ImportResolution = {
|
|
47
45
|
status: "unresolved", targets: [], reason: "target_not_found", coverage: "partial",
|
|
48
46
|
resolver_revision: RESOLVER_REVISION, config_revision: "", source: { file: sourceFile, target },
|
|
@@ -53,8 +51,13 @@ export class ImportResolver {
|
|
|
53
51
|
});
|
|
54
52
|
const read = (file: string): string | undefined => {
|
|
55
53
|
this.assertContained(file);
|
|
56
|
-
if (
|
|
57
|
-
|
|
54
|
+
if (contents.has(file)) return contents.get(file);
|
|
55
|
+
if (probeSourcePath(file) === "missing") { manifests[path.relative(this.root, file)] = "missing"; return undefined; }
|
|
56
|
+
const text = readSourceFile(file);
|
|
57
|
+
bytesRead += Buffer.byteLength(text, "utf8");
|
|
58
|
+
if (bytesRead > 16 * 1024 * 1024) throw new Error("resolution_source_byte_limit");
|
|
59
|
+
contents.set(file, text);
|
|
60
|
+
manifests[path.relative(this.root, file)] = computeContentHash(text); return text;
|
|
58
61
|
};
|
|
59
62
|
const json = (file: string): any => {
|
|
60
63
|
const raw = read(file); if (raw === undefined) return {};
|
|
@@ -83,7 +86,7 @@ export class ImportResolver {
|
|
|
83
86
|
const candidates = [base, ...extraCandidates, ...extensions.map(ext => base + ext), ...extensions.map(ext => path.join(base, "index" + ext))];
|
|
84
87
|
return [...new Set(candidates.filter(file => {
|
|
85
88
|
this.assertContained(file);
|
|
86
|
-
return
|
|
89
|
+
return probeSourcePath(file) === "file";
|
|
87
90
|
}).map(file => path.relative(this.root, file).replace(/\\/g, "/")))];
|
|
88
91
|
};
|
|
89
92
|
const resolvedFiles = (files: string[], reason: string) => {
|
|
@@ -95,45 +98,30 @@ export class ImportResolver {
|
|
|
95
98
|
if (target.startsWith("dynamic:")) return finish("unresolved", "dynamic_target");
|
|
96
99
|
const source = path.resolve(this.root, sourceFile); this.assertContained(source);
|
|
97
100
|
const ext = path.extname(source).toLowerCase();
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
101
|
+
const io: ResolutionIO = { root: this.root, read, check: file => this.assertContained(file), finish };
|
|
102
|
+
if (ext === ".go") return resolveGo(io, target, sourceFile, GO_STDLIB);
|
|
103
|
+
if (ext === ".py") return resolvePython(io, target, sourceFile, this.packageRoots, PYTHON_STDLIB);
|
|
104
|
+
if (ext === ".rb") return resolveRuby(io, target, sourceFile, statement);
|
|
105
|
+
if (ext === ".rs") return resolveRust(io, target, sourceFile);
|
|
106
|
+
if (ext === ".java") return resolveJava(io, target, sourceFile, statement);
|
|
107
|
+
if (ext === ".cs") return resolveCSharp(io, target, sourceFile, statement);
|
|
108
|
+
if (ext === ".php") {
|
|
109
|
+
let rawTarget = target.trim();
|
|
110
|
+
const useMatch = rawTarget.match(/^use\s+(?:function\s+|const\s+)?([^;\s]+)(?:\s+as\s+[^;\s]+)?\s*;?$/i);
|
|
111
|
+
if (useMatch) {
|
|
112
|
+
rawTarget = useMatch[1]!;
|
|
113
|
+
} else {
|
|
114
|
+
rawTarget = rawTarget.replace(/;+$/, "").trim();
|
|
106
115
|
}
|
|
107
|
-
if (
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
const packageDir = path.resolve(modDir, target.slice(moduleName.length).replace(/^\//, "")); this.assertContained(packageDir);
|
|
113
|
-
let parent = packageDir;
|
|
114
|
-
while (parent !== modDir) {
|
|
115
|
-
if (!parent.startsWith(modDir + path.sep)) return finish("unsupported", "outside_module");
|
|
116
|
-
if (read(path.join(parent, "go.mod")) !== undefined) return finish("unsupported", "nested_go_mod");
|
|
117
|
-
parent = path.dirname(parent);
|
|
116
|
+
if (rawTarget.endsWith(".php")) {
|
|
117
|
+
const absPhp = path.resolve(this.root, rawTarget.replace(/^\.\//, ""));
|
|
118
|
+
this.assertContained(absPhp);
|
|
119
|
+
if (probeSourcePath(absPhp) === "file") {
|
|
120
|
+
return resolvedFiles([path.relative(this.root, absPhp).replace(/\\/g, "/")], "php_file_path");
|
|
118
121
|
}
|
|
119
|
-
|
|
120
|
-
const names = fs.readdirSync(packageDir).filter(name => name.endsWith(".go") && !name.endsWith("_test.go")).sort();
|
|
121
|
-
const packageFiles: string[] = [];
|
|
122
|
-
for (const name of names) {
|
|
123
|
-
const file = path.join(packageDir, name); this.assertContained(file);
|
|
124
|
-
const content = readSourceFile(file);
|
|
125
|
-
if (/^\s*\/\/\s*(?:go:build|\+build)\b/m.test(content) || /_(?:linux|windows|darwin|freebsd|amd64|arm64|386|arm)\.go$/.test(name)) result.coverage = "conditional";
|
|
126
|
-
packageFiles.push(path.relative(this.root, file).replace(/\\/g, "/"));
|
|
127
|
-
}
|
|
128
|
-
return packageFiles.length ? finish("resolved", "go_module_package", [{ kind: "package", files: packageFiles }]) : finish("unresolved", "go_package_empty");
|
|
122
|
+
return finish("unresolved", "file_not_found");
|
|
129
123
|
}
|
|
130
|
-
|
|
131
|
-
const requirements = [...mod.matchAll(/^\s*(?:require\s+)?([A-Za-z0-9._~/-]+)\s+v[^\s]+/gm)].map(m => m[1]!);
|
|
132
|
-
if (requirements.some(name => target === name || target.startsWith(name + "/"))) return finish("external", "go_required_module");
|
|
133
|
-
return finish("unresolved", "go_module_not_declared");
|
|
134
|
-
}
|
|
135
|
-
if (ext === ".php") {
|
|
136
|
-
const normTarget = target.replace(/^\\/, "");
|
|
124
|
+
const normTarget = rawTarget.replace(/^\\/, "");
|
|
137
125
|
if (
|
|
138
126
|
PHP_STDLIB.has(normTarget) ||
|
|
139
127
|
normTarget.startsWith("Attribute") ||
|
|
@@ -142,7 +130,15 @@ export class ImportResolver {
|
|
|
142
130
|
return finish("external", "php_standard_library");
|
|
143
131
|
}
|
|
144
132
|
|
|
145
|
-
|
|
133
|
+
let composerDir = this.root;
|
|
134
|
+
for (let dir = path.dirname(source); dir === this.root || dir.startsWith(this.root + path.sep); dir = path.dirname(dir)) {
|
|
135
|
+
if (probeSourcePath(path.join(dir, "composer.json")) !== "missing") {
|
|
136
|
+
composerDir = dir;
|
|
137
|
+
break;
|
|
138
|
+
}
|
|
139
|
+
if (dir === this.root) break;
|
|
140
|
+
}
|
|
141
|
+
const composer = json(path.join(composerDir, "composer.json"));
|
|
146
142
|
const rules = { ...(composer.autoload?.["psr-4"] ?? {}), ...(composer["autoload-dev"]?.["psr-4"] ?? {}) };
|
|
147
143
|
const matches = Object.keys(rules)
|
|
148
144
|
.filter((prefix) => normTarget.startsWith(prefix.replace(/^\\/, "")))
|
|
@@ -157,7 +153,7 @@ export class ImportResolver {
|
|
|
157
153
|
const relativePath = normTarget.slice(cleanPrefix.length).replace(/\\/g, "/");
|
|
158
154
|
const candidateFiles: string[] = [];
|
|
159
155
|
for (const dir of dirs) {
|
|
160
|
-
const absBase = path.resolve(
|
|
156
|
+
const absBase = path.resolve(composerDir, dir, relativePath);
|
|
161
157
|
this.assertContained(absBase);
|
|
162
158
|
candidateFiles.push(...filesAt(absBase, [".php"]));
|
|
163
159
|
}
|
|
@@ -168,6 +164,38 @@ export class ImportResolver {
|
|
|
168
164
|
return resolvedFiles(candidateFiles, "composer_psr4");
|
|
169
165
|
}
|
|
170
166
|
|
|
167
|
+
// Check installed Composer vendor PSR-4 autoload map (covers transitive & non-matching package names like Carbon\*, Inertia\*)
|
|
168
|
+
const autoloadPsr4 = read(path.join(composerDir, "vendor", "composer", "autoload_psr4.php"));
|
|
169
|
+
if (autoloadPsr4) {
|
|
170
|
+
for (const m of autoloadPsr4.matchAll(/['"]([^'"]+)['"]\s*=>/g)) {
|
|
171
|
+
const prefix = m[1]!.replace(/\\\\/g, "\\").replace(/^\\/, "");
|
|
172
|
+
if (prefix && normTarget.startsWith(prefix)) {
|
|
173
|
+
return finish("external", "composer_vendor_psr4");
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
// Fallback: check composer.lock PSR-4 declarations when vendor/ is absent
|
|
179
|
+
const composerLockRaw = read(path.join(composerDir, "composer.lock"));
|
|
180
|
+
if (composerLockRaw) {
|
|
181
|
+
const lock = JSON.parse(composerLockRaw);
|
|
182
|
+
const lockPackages = [
|
|
183
|
+
...(Array.isArray(lock?.packages) ? lock.packages : []),
|
|
184
|
+
...(Array.isArray(lock?.["packages-dev"]) ? lock["packages-dev"] : []),
|
|
185
|
+
];
|
|
186
|
+
for (const pkgEntry of lockPackages) {
|
|
187
|
+
const psr4 = pkgEntry?.autoload?.["psr-4"];
|
|
188
|
+
if (psr4 && typeof psr4 === "object") {
|
|
189
|
+
for (const prefix of Object.keys(psr4)) {
|
|
190
|
+
const cleanPrefix = prefix.replace(/^\\/, "");
|
|
191
|
+
if (cleanPrefix && normTarget.startsWith(cleanPrefix)) {
|
|
192
|
+
return finish("external", "composer_lock_package");
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
|
|
171
199
|
// Check declared composer dependencies
|
|
172
200
|
const requirements = { ...(composer.require ?? {}), ...(composer["require-dev"] ?? {}) };
|
|
173
201
|
const topNamespace = normTarget.split("\\")[0]!.toLowerCase();
|
|
@@ -195,130 +223,25 @@ export class ImportResolver {
|
|
|
195
223
|
|
|
196
224
|
return finish("unresolved", "php_namespace_not_mapped");
|
|
197
225
|
}
|
|
198
|
-
if (ext === ".py") {
|
|
199
|
-
if (target.startsWith(".")) {
|
|
200
|
-
const dotsMatch = target.match(/^\.+/);
|
|
201
|
-
const dotCount = dotsMatch ? dotsMatch[0].length : 0;
|
|
202
|
-
const remainder = target.slice(dotCount);
|
|
203
|
-
let baseDir = path.dirname(source);
|
|
204
|
-
for (let i = 1; i < dotCount; i++) {
|
|
205
|
-
baseDir = path.dirname(baseDir);
|
|
206
|
-
}
|
|
207
|
-
this.assertContained(baseDir);
|
|
208
|
-
const fsSubpath = remainder ? remainder.split(".").join(path.sep) : "";
|
|
209
|
-
const targetDirOrFile = fsSubpath ? path.resolve(baseDir, fsSubpath) : baseDir;
|
|
210
|
-
this.assertContained(targetDirOrFile);
|
|
211
|
-
|
|
212
|
-
const candidates: string[] = [];
|
|
213
|
-
if (fsSubpath) {
|
|
214
|
-
const pyFile = targetDirOrFile + ".py";
|
|
215
|
-
if (fs.existsSync(pyFile) && fs.statSync(pyFile).isFile()) {
|
|
216
|
-
candidates.push(path.relative(this.root, pyFile).replace(/\\/g, "/"));
|
|
217
|
-
}
|
|
218
|
-
}
|
|
219
|
-
const initFile = path.join(targetDirOrFile, "__init__.py");
|
|
220
|
-
if (fs.existsSync(initFile) && fs.statSync(initFile).isFile()) {
|
|
221
|
-
candidates.push(path.relative(this.root, initFile).replace(/\\/g, "/"));
|
|
222
|
-
}
|
|
223
|
-
return resolvedFiles(candidates, "python_relative_module");
|
|
224
|
-
}
|
|
225
|
-
|
|
226
|
-
const parts = target.split(".");
|
|
227
|
-
const topPackage = parts[0]!;
|
|
228
|
-
const fsSubpath = parts.join(path.sep);
|
|
229
|
-
|
|
230
|
-
const searchRoots: string[] = [];
|
|
231
|
-
const srcDir = path.resolve(this.root, "src");
|
|
232
|
-
if (fs.existsSync(srcDir) && fs.statSync(srcDir).isDirectory()) {
|
|
233
|
-
searchRoots.push(srcDir);
|
|
234
|
-
}
|
|
235
|
-
searchRoots.push(this.root);
|
|
236
|
-
const callerDir = path.dirname(source);
|
|
237
|
-
if (callerDir !== this.root && callerDir !== srcDir) {
|
|
238
|
-
searchRoots.push(callerDir);
|
|
239
|
-
}
|
|
240
|
-
for (const pkgRoot of this.packageRoots) {
|
|
241
|
-
const abs = path.resolve(this.root, pkgRoot);
|
|
242
|
-
if (!searchRoots.includes(abs) && fs.existsSync(abs)) {
|
|
243
|
-
searchRoots.push(abs);
|
|
244
|
-
}
|
|
245
|
-
}
|
|
246
|
-
|
|
247
|
-
const localCandidates: string[] = [];
|
|
248
|
-
for (const sRoot of searchRoots) {
|
|
249
|
-
const candidateFile = path.resolve(sRoot, fsSubpath + ".py");
|
|
250
|
-
if (candidateFile.startsWith(this.root + path.sep) && fs.existsSync(candidateFile) && fs.statSync(candidateFile).isFile()) {
|
|
251
|
-
localCandidates.push(path.relative(this.root, candidateFile).replace(/\\/g, "/"));
|
|
252
|
-
}
|
|
253
|
-
const candidateInit = path.resolve(sRoot, fsSubpath, "__init__.py");
|
|
254
|
-
if (candidateInit.startsWith(this.root + path.sep) && fs.existsSync(candidateInit) && fs.statSync(candidateInit).isFile()) {
|
|
255
|
-
localCandidates.push(path.relative(this.root, candidateInit).replace(/\\/g, "/"));
|
|
256
|
-
}
|
|
257
|
-
}
|
|
258
|
-
|
|
259
|
-
const unique = [...new Set(localCandidates)].sort();
|
|
260
|
-
if (unique.length > 0) {
|
|
261
|
-
return finish(
|
|
262
|
-
unique.length === 1 ? "resolved" : "ambiguous",
|
|
263
|
-
unique.length === 1 ? "python_local_module" : "multiple_python_candidates",
|
|
264
|
-
unique.map(file => ({ kind: "file", files: [file] }))
|
|
265
|
-
);
|
|
266
|
-
}
|
|
267
|
-
|
|
268
|
-
if (PYTHON_STDLIB.has(topPackage)) {
|
|
269
|
-
return finish("external", "python_standard_library");
|
|
270
|
-
}
|
|
271
|
-
|
|
272
|
-
const declaredPackages = new Set<string>();
|
|
273
|
-
const reqText = read(path.join(this.root, "requirements.txt"));
|
|
274
|
-
if (reqText) {
|
|
275
|
-
for (const rawLine of reqText.split(/\r?\n/)) {
|
|
276
|
-
const line = rawLine.replace(/#.*$/, "").trim();
|
|
277
|
-
if (!line || line.startsWith("-")) continue;
|
|
278
|
-
const match = line.match(/^([A-Za-z0-9_.-]+)/);
|
|
279
|
-
if (match) declaredPackages.add(match[1].toLowerCase().replace(/[-_.]/g, "_"));
|
|
280
|
-
}
|
|
281
|
-
}
|
|
282
|
-
const pyprojectText = read(path.join(this.root, "pyproject.toml"));
|
|
283
|
-
if (pyprojectText) {
|
|
284
|
-
for (const rawLine of pyprojectText.split(/\r?\n/)) {
|
|
285
|
-
const line = rawLine.replace(/#.*$/, "").trim();
|
|
286
|
-
const matchArray = line.match(/^["']([A-Za-z0-9_.-]+)(?:[><=~!^;].*)?["']/);
|
|
287
|
-
if (matchArray) {
|
|
288
|
-
declaredPackages.add(matchArray[1].toLowerCase().replace(/[-_.]/g, "_"));
|
|
289
|
-
continue;
|
|
290
|
-
}
|
|
291
|
-
const matchKey = line.match(/^([A-Za-z0-9_.-]+)\s*=\s*(?:\{|"[^"]*"|'[^']*'|\d)/);
|
|
292
|
-
if (matchKey && !["python", "version", "name", "description"].includes(matchKey[1].toLowerCase())) {
|
|
293
|
-
declaredPackages.add(matchKey[1].toLowerCase().replace(/[-_.]/g, "_"));
|
|
294
|
-
}
|
|
295
|
-
}
|
|
296
|
-
}
|
|
297
|
-
const pipfileText = read(path.join(this.root, "Pipfile"));
|
|
298
|
-
if (pipfileText) {
|
|
299
|
-
for (const rawLine of pipfileText.split(/\r?\n/)) {
|
|
300
|
-
const line = rawLine.replace(/#.*$/, "").trim();
|
|
301
|
-
const match = line.match(/^([A-Za-z0-9_.-]+)\s*=/);
|
|
302
|
-
if (match) declaredPackages.add(match[1].toLowerCase().replace(/[-_.]/g, "_"));
|
|
303
|
-
}
|
|
304
|
-
}
|
|
305
|
-
|
|
306
|
-
const normalizedTop = topPackage.toLowerCase().replace(/[-_.]/g, "_");
|
|
307
|
-
const isDeclared = declaredPackages.has(normalizedTop) ||
|
|
308
|
-
Object.entries(PYTHON_DIST_TO_MODULE_MAP).some(([dist, mods]) => declaredPackages.has(dist) && mods.includes(normalizedTop));
|
|
309
|
-
|
|
310
|
-
if (isDeclared) {
|
|
311
|
-
return finish("external", "python_manifest_dependency");
|
|
312
|
-
}
|
|
313
|
-
|
|
314
|
-
return finish("unresolved", "python_module_not_declared");
|
|
315
|
-
}
|
|
316
226
|
if (!/\.(?:[cm]?[jt]sx?|vue|astro|svelte)$/.test(ext)) return finish("unsupported", "language_resolver_unavailable");
|
|
317
227
|
const extensions = [".ts", ".tsx", ".mts", ".cts", ".js", ".jsx", ".mjs", ".cjs", ".d.ts", ".json"];
|
|
318
228
|
if (target.startsWith(".") || path.isAbsolute(target)) return resolvedFiles(filesAt(path.resolve(path.dirname(source), target), extensions), "relative_path");
|
|
319
229
|
if (builtinModules.includes(target) || target.startsWith("node:") && builtinModules.includes(target.slice(5)) || target === "bun" || target.startsWith("bun:")) return finish("external", "runtime_builtin");
|
|
320
|
-
|
|
321
|
-
|
|
230
|
+
let nearestTsConfig = path.join(this.root, "tsconfig.json");
|
|
231
|
+
let nearestPkgDir = this.root;
|
|
232
|
+
let foundTsConfig = false;
|
|
233
|
+
let foundPkg = false;
|
|
234
|
+
for (let dir = path.dirname(source); dir === this.root || dir.startsWith(this.root + path.sep); dir = path.dirname(dir)) {
|
|
235
|
+
if (!foundTsConfig && probeSourcePath(path.join(dir, "tsconfig.json")) !== "missing") {
|
|
236
|
+
nearestTsConfig = path.join(dir, "tsconfig.json");
|
|
237
|
+
foundTsConfig = true;
|
|
238
|
+
}
|
|
239
|
+
if (!foundPkg && probeSourcePath(path.join(dir, "package.json")) !== "missing") {
|
|
240
|
+
nearestPkgDir = dir;
|
|
241
|
+
foundPkg = true;
|
|
242
|
+
}
|
|
243
|
+
if (foundTsConfig && foundPkg) break;
|
|
244
|
+
if (dir === this.root) break;
|
|
322
245
|
}
|
|
323
246
|
const loadTsConfig = (configPath: string, visited = new Set<string>()): any => {
|
|
324
247
|
if (visited.has(configPath)) throw new Error("circular_tsconfig_extends");
|
|
@@ -333,12 +256,12 @@ export class ImportResolver {
|
|
|
333
256
|
: path.resolve(this.root, "node_modules", extRef);
|
|
334
257
|
this.assertContained(base);
|
|
335
258
|
let parentPath = base.endsWith(".json") ? base : base + ".json";
|
|
336
|
-
if (
|
|
259
|
+
if (probeSourcePath(parentPath) === "missing" && probeSourcePath(base) === "directory") {
|
|
337
260
|
const pkg = json(path.join(base, "package.json"));
|
|
338
261
|
parentPath = path.resolve(base, typeof pkg.tsconfig === "string" ? pkg.tsconfig : "tsconfig.json");
|
|
339
262
|
}
|
|
340
263
|
this.assertContained(parentPath);
|
|
341
|
-
if (
|
|
264
|
+
if (probeSourcePath(parentPath) === "missing") throw new Error("tsconfig_extends_not_found");
|
|
342
265
|
options = { ...options, ...loadTsConfig(parentPath, visited) };
|
|
343
266
|
}
|
|
344
267
|
const own = cfg.compilerOptions ?? {};
|
|
@@ -353,7 +276,7 @@ export class ImportResolver {
|
|
|
353
276
|
return options;
|
|
354
277
|
};
|
|
355
278
|
|
|
356
|
-
const options = loadTsConfig(
|
|
279
|
+
const options = loadTsConfig(nearestTsConfig);
|
|
357
280
|
const aliases = options.paths ?? {};
|
|
358
281
|
if (typeof aliases !== "object" || Array.isArray(aliases)) return finish("unsupported", "malformed_ts_paths");
|
|
359
282
|
const matching = Object.keys(aliases).filter(key => {
|
|
@@ -380,13 +303,14 @@ export class ImportResolver {
|
|
|
380
303
|
if (localFiles.length) return resolvedFiles(localFiles,"workspace_path");
|
|
381
304
|
}
|
|
382
305
|
const pkg = json(path.join(this.root,"package.json"));
|
|
306
|
+
const localPkg = nearestPkgDir !== this.root ? json(path.join(nearestPkgDir, "package.json")) : {};
|
|
383
307
|
const workspacePatterns = Array.isArray(pkg.workspaces) ? pkg.workspaces : pkg.workspaces?.packages ?? [];
|
|
384
308
|
const roots = [...this.packageRoots];
|
|
385
309
|
for (const pattern of workspacePatterns) {
|
|
386
310
|
if (typeof pattern !== "string") return finish("unsupported","malformed_workspace_pattern");
|
|
387
311
|
if (pattern.endsWith("/*")) {
|
|
388
312
|
const parent = path.resolve(this.root,pattern.slice(0,-2)); this.assertContained(parent);
|
|
389
|
-
if (
|
|
313
|
+
if (probeSourcePath(parent) !== "missing") roots.push(...readSourceDirectory(parent).filter(entry=>entry.isDirectory()).map(entry=>path.relative(this.root,path.join(parent,entry.name))));
|
|
390
314
|
} else if (!pattern.includes("*")) roots.push(pattern);
|
|
391
315
|
else return finish("unsupported","workspace_glob_not_supported");
|
|
392
316
|
}
|
|
@@ -420,7 +344,11 @@ export class ImportResolver {
|
|
|
420
344
|
}
|
|
421
345
|
if (workspaceFiles.length) return resolvedFiles(workspaceFiles,"workspace_package");
|
|
422
346
|
const name = target.startsWith("@") ? target.split("/").slice(0,2).join("/") : target.split("/")[0]!;
|
|
423
|
-
|
|
347
|
+
const depMaps = [
|
|
348
|
+
localPkg.dependencies, localPkg.devDependencies, localPkg.peerDependencies, localPkg.optionalDependencies,
|
|
349
|
+
pkg.dependencies, pkg.devDependencies, pkg.peerDependencies, pkg.optionalDependencies,
|
|
350
|
+
];
|
|
351
|
+
if (depMaps.some(deps => deps && Object.hasOwn(deps, name))) return finish("external", "package_manifest_dependency");
|
|
424
352
|
return finish("unresolved", "package_not_declared");
|
|
425
353
|
} catch (error) {
|
|
426
354
|
return finish("unsupported", error instanceof Error && error.message.includes("outside_workspace") ? "outside_workspace" : `manifest_or_source_error: ${error instanceof Error ? error.message : String(error)}`);
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { basename } from "node:path";
|
|
2
|
+
|
|
3
|
+
/** Files whose staged bytes and watcher events can change import interpretation. */
|
|
4
|
+
export function isResolutionInput(file: string): boolean {
|
|
5
|
+
return /\.(?:json|jsonc|toml|ya?ml|xml|csproj|props|targets|gradle|gradle\.kts|gemspec|lock|txt|in)$/i.test(file) ||
|
|
6
|
+
/^(?:Gemfile|Pipfile|go\.mod|go\.work|artisan|nest-cli\.json)$/.test(basename(file));
|
|
7
|
+
}
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
import * as path from "node:path";
|
|
2
|
+
import { probeSourcePath, readSourceDirectory } from "../../resources/source-files.ts";
|
|
3
|
+
import { ancestors, packageResult, type ResolutionIO } from "./shared.ts";
|
|
4
|
+
|
|
5
|
+
interface Replace { from: string; version?: string; to: string; remoteVersion?: string; directory: string }
|
|
6
|
+
function tokens(line: string): string[] {
|
|
7
|
+
const clean = line.replace(/"(?:\\.|[^"\\])*"|\/\/.*$/g, token => token.startsWith('"') ? token : "");
|
|
8
|
+
return (clean.match(/"(?:\\.|[^"\\])*"|[^\s]+/g) ?? []).map(token => token.startsWith('"') ? JSON.parse(token) : token);
|
|
9
|
+
}
|
|
10
|
+
function directives(text: string, kind: string): string[][] {
|
|
11
|
+
const result: string[][] = []; let block = false;
|
|
12
|
+
for (const line of text.split(/\r?\n/)) {
|
|
13
|
+
const parts = tokens(line); if (!parts.length) continue;
|
|
14
|
+
if (parts[0] === ")") { block = false; continue; }
|
|
15
|
+
if (parts[0] === kind) {
|
|
16
|
+
if (parts[1] === "(") block = true;
|
|
17
|
+
else result.push(parts.slice(1));
|
|
18
|
+
} else if (block) result.push(parts);
|
|
19
|
+
}
|
|
20
|
+
return result;
|
|
21
|
+
}
|
|
22
|
+
function replacements(text: string, directory: string): Replace[] {
|
|
23
|
+
return directives(text, "replace").map(parts => {
|
|
24
|
+
const arrow = parts.indexOf("=>");
|
|
25
|
+
if (![1, 2].includes(arrow) || ![arrow + 2, arrow + 3].includes(parts.length)) throw new Error("malformed_go_replace");
|
|
26
|
+
return { from: parts[0]!, version: arrow === 2 ? parts[1] : undefined, to: parts[arrow + 1]!, remoteVersion: parts[arrow + 2], directory };
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
function moduleName(text: string): string {
|
|
30
|
+
const names = directives(text, "module");
|
|
31
|
+
if (names.length !== 1 || names[0]!.length !== 1) throw new Error("malformed_go_mod");
|
|
32
|
+
return names[0]![0]!;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export function resolveGo(io: ResolutionIO, target: string, source: string, stdlib: ReadonlySet<string>) {
|
|
36
|
+
const dirs = ancestors(io, source);
|
|
37
|
+
const mods = dirs.map(dir => ({ dir, text: io.read(path.join(dir, "go.mod")) }));
|
|
38
|
+
const own = mods.find(mod => mod.text !== undefined);
|
|
39
|
+
if (!own) return io.finish("unsupported", "go_mod_missing");
|
|
40
|
+
const ownName = moduleName(own.text!);
|
|
41
|
+
const matches = (name: string) => target === name || target.startsWith(name + "/");
|
|
42
|
+
if (!matches(ownName) && stdlib.has(target.split("/")[0]!)) return io.finish("external", "go_standard_library");
|
|
43
|
+
const works = dirs.map(dir => ({ dir, text: io.read(path.join(dir, "go.work")) }));
|
|
44
|
+
const work = works.find(work => work.text !== undefined);
|
|
45
|
+
const modules = new Map<string, string[]>();
|
|
46
|
+
const addModule = (dir: string, text?: string) => {
|
|
47
|
+
io.check(dir);
|
|
48
|
+
const content = text ?? io.read(path.join(dir, "go.mod"));
|
|
49
|
+
if (content === undefined) throw new Error("go_workspace_module_missing");
|
|
50
|
+
const name = moduleName(content);
|
|
51
|
+
modules.set(name, [...new Set([...(modules.get(name) ?? []), dir])]);
|
|
52
|
+
};
|
|
53
|
+
if (work) {
|
|
54
|
+
for (const use of directives(work.text!, "use")) {
|
|
55
|
+
if (use.length !== 1) throw new Error("malformed_go_work_use");
|
|
56
|
+
addModule(path.resolve(work.dir, use[0]!));
|
|
57
|
+
}
|
|
58
|
+
if (!modules.get(ownName)?.includes(own.dir)) return io.finish("unsupported", "go_source_module_not_in_workspace");
|
|
59
|
+
} else addModule(own.dir, own.text);
|
|
60
|
+
const required = new Map(directives(own.text!, "require").map(parts => [parts[0]!, parts[1]]));
|
|
61
|
+
const moduleReplaces = replacements(own.text!, own.dir);
|
|
62
|
+
const workReplaces = work ? replacements(work.text!, work.dir) : [];
|
|
63
|
+
const allNames = [...new Set([...modules.keys(), ...moduleReplaces.map(r => r.from), ...workReplaces.map(r => r.from)])]
|
|
64
|
+
.filter(matches).sort((a, b) => b.length - a.length);
|
|
65
|
+
const name = allNames[0];
|
|
66
|
+
if (name) {
|
|
67
|
+
const select = (entries: Replace[]) => entries.filter(r => r.from === name && (!r.version || r.version === required.get(name)))
|
|
68
|
+
.sort((a, b) => Number(Boolean(b.version)) - Number(Boolean(a.version)));
|
|
69
|
+
const candidates = select(workReplaces).length ? select(workReplaces) : select(moduleReplaces);
|
|
70
|
+
const replacement = candidates[0];
|
|
71
|
+
if (candidates.length > 1 && candidates[1]!.version === replacement!.version) return io.finish("ambiguous", "multiple_go_replacements");
|
|
72
|
+
let roots = modules.get(name) ?? [];
|
|
73
|
+
if (replacement) {
|
|
74
|
+
if (!modules.has(name) && !required.has(name)) return io.finish("unresolved", "go_module_not_required");
|
|
75
|
+
if (replacement.remoteVersion) return required.has(name) ? io.finish("external", "go_replaced_required_module") : io.finish("unresolved", "go_module_not_required");
|
|
76
|
+
if (!replacement.to.startsWith(".") && !path.isAbsolute(replacement.to)) return io.finish("unsupported", "go_replace_target_not_local");
|
|
77
|
+
roots = [path.resolve(replacement.directory, replacement.to)];
|
|
78
|
+
io.check(roots[0]!);
|
|
79
|
+
if (io.read(path.join(roots[0]!, "go.mod")) === undefined) return io.finish("unresolved", "go_replacement_module_missing");
|
|
80
|
+
}
|
|
81
|
+
if (roots.length !== 1) return io.finish(roots.length ? "ambiguous" : "unresolved", "go_module_root_not_unique");
|
|
82
|
+
const root = roots[0]!;
|
|
83
|
+
const directory = path.resolve(root, target.slice(name.length).replace(/^\//, "")); io.check(directory);
|
|
84
|
+
for (let dir = directory; dir !== root; dir = path.dirname(dir)) {
|
|
85
|
+
if (!dir.startsWith(root + path.sep)) return io.finish("unsupported", "outside_module");
|
|
86
|
+
if (io.read(path.join(dir, "go.mod")) !== undefined) return io.finish("unsupported", "nested_go_mod");
|
|
87
|
+
}
|
|
88
|
+
if (probeSourcePath(directory) !== "directory") return io.finish("unresolved", "go_package_missing");
|
|
89
|
+
const files: string[] = []; let conditional = false;
|
|
90
|
+
for (const entry of readSourceDirectory(directory)) {
|
|
91
|
+
if (!entry.name.endsWith(".go") || entry.name.endsWith("_test.go")) continue;
|
|
92
|
+
const file = path.join(directory, entry.name); io.check(file);
|
|
93
|
+
const content = io.read(file); if (content === undefined) continue;
|
|
94
|
+
conditional ||= /^\s*\/\/\s*(?:go:build|\+build)\b/m.test(content) || /_(?:linux|windows|darwin|freebsd|openbsd|netbsd|android|ios|amd64|arm64|386|arm)(?:_(?:amd64|arm64|386|arm))?\.go$/.test(entry.name);
|
|
95
|
+
files.push(path.relative(io.root, file).replace(/\\/g, "/"));
|
|
96
|
+
}
|
|
97
|
+
const result = packageResult(io, files, files.length ? "go_module_package" : "go_package_empty");
|
|
98
|
+
if (conditional) result.coverage = "conditional";
|
|
99
|
+
return result;
|
|
100
|
+
}
|
|
101
|
+
if ([...required.keys()].some(matches)) return io.finish("external", "go_required_module");
|
|
102
|
+
return io.finish("unresolved", "go_module_not_declared");
|
|
103
|
+
}
|