Skip to content

Add allow_external flag & Fix Tests #807

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

Merged
merged 12 commits into from
Mar 12, 2025
13 changes: 13 additions & 0 deletions docs/introduction/advanced-settings.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,19 @@ Controls import path overrides during import resolution.

Enables and disables resolution of imports from `sys.path`.

<Warning>
For this to properly work, you must also set `allow_external` to `True`.
</Warning>

## Flag: `allow_external`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm I don't think we have the performance in the current sdk to handle external resolution so this makes sense

> **Default: `False`**

Enables resolving imports, files, modules, and directories from outside of the repo path.

<Warning>
Turning this flag off may allow for bad actors to access files outside of the repo path! Use with caution!
</Warning>

## Flag: `ts_dependency_manager`
> **Default: `False`**

Expand Down
1 change: 1 addition & 0 deletions src/codegen/configs/models/codebase.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ def __init__(self, prefix: str = "CODEBASE", *args, **kwargs) -> None:
import_resolution_paths: list[str] = Field(default_factory=lambda: [])
import_resolution_overrides: dict[str, str] = Field(default_factory=lambda: {})
py_resolve_syspath: bool = False
allow_external: bool = False
ts_dependency_manager: bool = False
ts_language_engine: bool = False
v8_ts_engine: bool = False
Expand Down
4 changes: 2 additions & 2 deletions src/codegen/sdk/codebase/codebase_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,7 @@ def get_directory(self, directory_path: PathLike, create_on_missing: bool = Fals
"""
# If not part of repo path, return None
absolute_path = self.to_absolute(directory_path)
if not self.is_subdir(absolute_path):
if not self.is_subdir(absolute_path) and not self.config.allow_external:
assert False, f"Directory {absolute_path} is not part of repo path {self.repo_path}"
return None

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

# Check if file exists in graph
Expand Down
7 changes: 7 additions & 0 deletions src/codegen/sdk/core/codebase.py
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,13 @@ def __init__(
self.ctx = CodebaseContext(projects, config=config, secrets=secrets, io=io, progress=progress)
self.console = Console(record=True, soft_wrap=True)

# Assert config assertions
# External import resolution must be enabled if syspath is enabled
if self.ctx.config.py_resolve_syspath:
if not self.ctx.config.allow_external:
msg = "allow_external must be set to True when py_resolve_syspath is enabled"
raise ValueError(msg)

@noapidoc
def __str__(self) -> str:
return f"<Codebase(name={self.name}, language={self.language}, path={self.repo_path})>"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,8 @@ def func():

# Enable resolution via sys.path
codebase.ctx.config.py_resolve_syspath = True
# Allow resolving files and modules outside of the repo path
codebase.ctx.config.allow_external = True

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

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

# =====[ Default import works ]=====
assert len(consumer_file.imports) == 1
Expand Down
Loading