Skip to content

Commit c19a7a6

Browse files
EdwardJXLikopekCtomcodgentkfossfrainfreeze
authored
Add allow_external flag & Fix Tests (#807)
# Motivation <!-- Why is this change necessary? --> # Content <!-- Please include a summary of the change --> # Testing <!-- How was the change tested? --> # Please check the following before marking your PR as ready for review - [ ] I have added tests for my changes - [ ] I have updated the documentation or added new documentation as needed --------- Co-authored-by: Edo Pujol <[email protected]> Co-authored-by: kopekC <[email protected]> Co-authored-by: tomcodgen <[email protected]> Co-authored-by: tomcodgen <[email protected]> Co-authored-by: tomcodegen <[email protected]>
1 parent 34e4148 commit c19a7a6

File tree

5 files changed

+29
-2
lines changed

5 files changed

+29
-2
lines changed

docs/introduction/advanced-settings.mdx

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,19 @@ Controls import path overrides during import resolution.
309309
310310
Enables and disables resolution of imports from `sys.path`.
311311

312+
<Warning>
313+
For this to properly work, you must also set `allow_external` to `True`.
314+
</Warning>
315+
316+
## Flag: `allow_external`
317+
> **Default: `False`**
318+
319+
Enables resolving imports, files, modules, and directories from outside of the repo path.
320+
321+
<Warning>
322+
Turning this flag off may allow for bad actors to access files outside of the repo path! Use with caution!
323+
</Warning>
324+
312325
## Flag: `ts_dependency_manager`
313326
> **Default: `False`**
314327

src/codegen/configs/models/codebase.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ def __init__(self, prefix: str = "CODEBASE", *args, **kwargs) -> None:
2121
import_resolution_paths: list[str] = Field(default_factory=lambda: [])
2222
import_resolution_overrides: dict[str, str] = Field(default_factory=lambda: {})
2323
py_resolve_syspath: bool = False
24+
allow_external: bool = False
2425
ts_dependency_manager: bool = False
2526
ts_language_engine: bool = False
2627
v8_ts_engine: bool = False

src/codegen/sdk/codebase/codebase_context.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -381,7 +381,7 @@ def get_directory(self, directory_path: PathLike, create_on_missing: bool = Fals
381381
"""
382382
# If not part of repo path, return None
383383
absolute_path = self.to_absolute(directory_path)
384-
if not self.is_subdir(absolute_path):
384+
if not self.is_subdir(absolute_path) and not self.config.allow_external:
385385
assert False, f"Directory {absolute_path} is not part of repo path {self.repo_path}"
386386
return None
387387

@@ -611,7 +611,7 @@ def get_edges(self) -> list[tuple[NodeId, NodeId, EdgeType, Usage | None]]:
611611
def get_file(self, file_path: os.PathLike, ignore_case: bool = False) -> SourceFile | None:
612612
# If not part of repo path, return None
613613
absolute_path = self.to_absolute(file_path)
614-
if not self.is_subdir(absolute_path):
614+
if not self.is_subdir(absolute_path) and not self.config.allow_external:
615615
assert False, f"File {file_path} is not part of the repository path"
616616

617617
# Check if file exists in graph

src/codegen/sdk/core/codebase.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,13 @@ def __init__(
213213
self.ctx = CodebaseContext(projects, config=config, secrets=secrets, io=io, progress=progress)
214214
self.console = Console(record=True, soft_wrap=True)
215215

216+
# Assert config assertions
217+
# External import resolution must be enabled if syspath is enabled
218+
if self.ctx.config.py_resolve_syspath:
219+
if not self.ctx.config.allow_external:
220+
msg = "allow_external must be set to True when py_resolve_syspath is enabled"
221+
raise ValueError(msg)
222+
216223
@noapidoc
217224
def __str__(self) -> str:
218225
return f"<Codebase(name={self.name}, language={self.language}, path={self.repo_path})>"

tests/unit/codegen/sdk/python/import_resolution/test_import_resolution.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,8 @@ def func():
277277

278278
# Enable resolution via sys.path
279279
codebase.ctx.config.py_resolve_syspath = True
280+
# Allow resolving files and modules outside of the repo path
281+
codebase.ctx.config.allow_external = True
280282

281283
# =====[ Imports cannot be found without sys.path being set ]=====
282284
assert len(consumer_file.imports) == 1
@@ -372,6 +374,8 @@ def func():
372374
# Ensure we don't have overrites and enable syspath resolution
373375
codebase.ctx.config.import_resolution_paths = []
374376
codebase.ctx.config.py_resolve_syspath = True
377+
# Allow resolving files and modules outside of the repo path
378+
codebase.ctx.config.allow_external = True
375379

376380
# =====[ Import with sys.path set can be found ]=====
377381
assert len(consumer_file.imports) == 1
@@ -419,6 +423,8 @@ def func():
419423
# Ensure we don't have overrites and enable syspath resolution
420424
codebase.ctx.config.import_resolution_paths = []
421425
codebase.ctx.config.py_resolve_syspath = True
426+
# Allow resolving files and modules outside of the repo path
427+
codebase.ctx.config.allow_external = True
422428

423429
# =====[ Default import works ]=====
424430
assert len(consumer_file.imports) == 1

0 commit comments

Comments
 (0)