Skip to content

Commit c7edae2

Browse files
committed
fix: exclude local modules from scan command
1 parent 02c0a47 commit c7edae2

2 files changed

Lines changed: 37 additions & 1 deletion

File tree

src/utils/nativeScan.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -588,7 +588,24 @@ export async function scanFileForImports(filePath: string): Promise<Set<string>>
588588
addTopLevelImport(moduleName, imports);
589589
}
590590

591-
return imports;
591+
// Filter out local modules
592+
const fileDir = path.dirname(filePath);
593+
const checks = await Promise.all(
594+
Array.from(imports).map(async (pkg) => ({
595+
pkg,
596+
isLocal: await pathExists(path.join(fileDir, `${pkg}.py`)),
597+
}))
598+
);
599+
600+
const filteredImports = new Set<string>();
601+
for (const { pkg, isLocal } of checks) {
602+
if (!isLocal) {
603+
// Not a local module, keep it in the imports
604+
filteredImports.add(pkg);
605+
}
606+
}
607+
608+
return filteredImports;
592609
}
593610

594611
function addTopLevelImport(qualified: string, into: Set<string>): void {

test/utils/nativeScan.test.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,25 @@ describe('nativeScan: scanFileForImports', () => {
267267
expect([...imports].sort()).to.deep.equal(['numpy', 'pandas']);
268268
await fs.rm(tmp, { recursive: true, force: true });
269269
});
270+
271+
it('filters out local modules that exist as .py files in the same directory', async () => {
272+
const tmp = await fs.mkdtemp(path.join(os.tmpdir(), 'imports-'));
273+
const file = path.join(tmp, 'entrypoint.py');
274+
// Create a local module file that should be filtered out
275+
await fs.writeFile(path.join(tmp, 'helper.py'), '# local helper module\n');
276+
await fs.writeFile(
277+
file,
278+
[
279+
'import pandas',
280+
'import numpy',
281+
'import helper', // local module - should be filtered
282+
'from helper import some_function', // also local - should be filtered
283+
].join('\n')
284+
);
285+
const imports = await scanFileForImports(file);
286+
expect([...imports].sort()).to.deep.equal(['numpy', 'pandas']);
287+
await fs.rm(tmp, { recursive: true, force: true });
288+
});
270289
});
271290

272291
describe('nativeScan: writeRequirementsFile', () => {

0 commit comments

Comments
 (0)