MCPcopy Create free account
hub / github.com/colbymchenry/codegraph / isExternalImport

Function isExternalImport

src/resolution/import-resolver.ts:1281–1364  ·  view source on GitHub ↗
(
  importPath: string,
  language: Language,
  context?: ResolutionContext,
  options: { aliasPrefixes?: boolean; fromFile?: string } = {}
)

Source from the content-addressed store, hash-verified

1279 * files under the manifest that declares it (#2456).
1280 */
1281export function isExternalImport(
1282 importPath: string,
1283 language: Language,
1284 context?: ResolutionContext,
1285 options: { aliasPrefixes?: boolean; fromFile?: string } = {}
1286): boolean {
1287 // Relative imports are not external
1288 if (importPath.startsWith('.')) {
1289 return false;
1290 }
1291
1292 // Workspace-member imports (`@scope/ui`, `@scope/ui/widgets`) are LOCAL to
1293 // a monorepo even though they look like bare npm specifiers. Consult the
1294 // workspace map first so they aren't misclassified as external (#629). The
1295 // map is null for single-package repos, so this is a no-op there.
1296 const workspaces = context?.getWorkspacePackages?.();
1297 if (workspaces && resolveWorkspaceImport(importPath, workspaces, options.fromFile)) {
1298 return false;
1299 }
1300
1301 // Common external patterns
1302 if (ESM_IMPORT_LANGUAGES.has(language)) {
1303 // Node built-ins
1304 if (['fs', 'path', 'os', 'crypto', 'http', 'https', 'url', 'util', 'events', 'stream', 'child_process', 'buffer'].includes(importPath)) {
1305 return true;
1306 }
1307 // Project-defined alias prefix? Treat as local. A catch-all `"*"` has no
1308 // prefix and matches `react` too: what it names is left to
1309 // resolveFromOwnConfig, which keeps the app's packages out.
1310 const aliases = options.aliasPrefixes === false ? null : context?.getProjectAliases?.();
1311 if (aliases) {
1312 for (const pat of aliases.patterns) {
1313 if (pat.prefix !== '' && importPath.startsWith(pat.prefix)) return false;
1314 }
1315 }
1316 // Scoped packages or bare specifiers that don't start with aliases
1317 if (!importPath.startsWith('@/') && !importPath.startsWith('~/') && !importPath.startsWith('src/')) {
1318 // Likely an npm package
1319 return true;
1320 }
1321 }
1322
1323 if (language === 'python') {
1324 // Standard library modules
1325 const stdLibs = ['os', 'sys', 'json', 're', 'math', 'datetime', 'collections', 'typing', 'pathlib', 'logging'];
1326 if (stdLibs.includes(importPath.split('.')[0]!)) {
1327 return true;
1328 }
1329 }
1330
1331 if (language === 'go') {
1332 // Relative imports (rare in idiomatic Go but the grammar allows them).
1333 if (importPath.startsWith('.')) {
1334 return false;
1335 }
1336 // In-module imports look like `<module-path>/sub/pkg` — local to
1337 // this project. Without the module-path check we'd flag every
1338 // cross-package call in a Go monorepo as external (issue #388) — and,

Callers 5

createContextMethod · 0.90
isBoundToOutOfRepoImportFunction · 0.85
importBindingTailFunction · 0.85
pythonImportBindingTailFunction · 0.85

Calls 5

resolveWorkspaceImportFunction · 0.90
getWorkspacePackagesMethod · 0.80
hasMethod · 0.80
getProjectAliasesMethod · 0.80
getGoPackageDirMethod · 0.80

Tested by

no test coverage detected