Skip to content

Create the temporary virtualenv outside the analysis target - #331

Open
bjk7119 wants to merge 4 commits into
mainfrom
pypi
Open

Create the temporary virtualenv outside the analysis target#331
bjk7119 wants to merge 4 commits into
mainfrom
pypi

Conversation

@bjk7119

@bjk7119 bjk7119 commented Jul 31, 2026

Copy link
Copy Markdown
Contributor

Summary by CodeRabbit

Bug Fixes

  • Improved handling of Python virtual-environment activation commands across supported shell formats.
  • Fixed issues with activation paths containing special characters or spaces.
  • Improved temporary environment isolation by using unique locations.
  • Enhanced virtual-environment creation and package inspection across Windows and POSIX platforms.
  • Added clearer failure reporting with safely redacted and size-limited command output when setup fails.
  • Improved protection of sensitive credentials and private package-index URLs in error logs.

@bjk7119 bjk7119 self-assigned this Jul 31, 2026
@bjk7119 bjk7119 added the bug [Issue] Something isn't working label Jul 31, 2026
@coderabbitai

coderabbitai Bot commented Jul 31, 2026

Copy link
Copy Markdown

Review Change Stack

📝 Walkthrough

Walkthrough

The PyPI package manager now redacts command errors and private-index credentials, limits stderr output, quotes shell paths, and detects virtual-environment failures. It stores virtual environments in exclusive system temporary directories and supports Windows and POSIX site-package layouts.

Changes

PyPI virtual environment handling

Layer / File(s) Summary
Command quoting and failure reporting
src/fosslight_dependency/package_manager/Pypi.py
Adds secret redaction, bounded stderr extraction, failure classification, shell-path quoting, activation-command quoting, and private-index URL redaction. Applies the updated handling to creation and inspection commands.
Temporary virtual environment lifecycle
src/fosslight_dependency/package_manager/Pypi.py
Creates virtual environments in exclusive system temporary directories. Updates site-package lookup for Windows and POSIX layouts and quotes creation paths.

Estimated code review effort: 3 (Moderate) | ~25 minutes

Suggested reviewers: woocheol-lge

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 22.22% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly describes the pull request's primary change: placing the temporary virtualenv outside the analysis target.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches 💡 1
📝 Generate docstrings 💡
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch pypi

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@bjk7119

bjk7119 commented Jul 31, 2026

Copy link
Copy Markdown
Contributor Author

@coderabbitai review

@coderabbitai

coderabbitai Bot commented Jul 31, 2026

Copy link
Copy Markdown
✅ Action performed

Review finished.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Actionable comments posted: 3

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@src/fosslight_dependency/package_manager/Pypi.py`:
- Line 82: Update the direct lookup in get_license_from_file() after assigning
venv_path to check os.path.join(venv_path, 'Lib', 'site-packages') before the
existing POSIX-style lib/pythonX/site-packages candidate, preserving the current
fallback behavior.
- Around line 57-62: Update the Pypi instance initialization that assigns
self.venv_tmp_dir to create an exclusive per-instance temporary directory using
tempfile.mkdtemp(), rather than deriving a shared path from the process ID.
Preserve get_virtualenv_site_packages() and __del__() behavior while ensuring
each instance has an isolated path and cleanup cannot race with other instances.
- Around line 25-39: Update quote_activate_cmd and both venv creation and pip
inspection command chains to prevent shell metacharacter, command-substitution,
and environment-variable expansion for interpolated paths. Prefer removing
shell=True and invoking virtualenv/Python/pip directly; otherwise apply escaping
appropriate to the active platform and shell before constructing command chains.
Preserve support for source, dot, and conda activation forms.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro Plus

Run ID: 990576e9-11ba-410c-af28-d2a2b66fb1af

📥 Commits

Reviewing files that changed from the base of the PR and between cd7a025 and ef3cf43.

📒 Files selected for processing (1)
  • src/fosslight_dependency/package_manager/Pypi.py

Comment thread src/fosslight_dependency/package_manager/Pypi.py Outdated
Comment thread src/fosslight_dependency/package_manager/Pypi.py Outdated
Comment thread src/fosslight_dependency/package_manager/Pypi.py

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@src/fosslight_dependency/package_manager/Pypi.py`:
- Around line 28-42: Update describe_venv_failure() to sanitize stderr_text
before returning it, redacting credential-bearing URLs and secret values while
preserving useful failure context and truncation behavior. Ensure every
stderr-derived message returned to create_virtualenv() is safe for logging;
alternatively return only a generic failure message when redaction cannot be
reliably applied.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro Plus

Run ID: 874fde6f-55be-4717-9aee-cfe5af947e0b

📥 Commits

Reviewing files that changed from the base of the PR and between ef3cf43 and 83c18c0.

📒 Files selected for processing (1)
  • src/fosslight_dependency/package_manager/Pypi.py

Comment thread src/fosslight_dependency/package_manager/Pypi.py

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
src/fosslight_dependency/package_manager/Pypi.py (1)

95-132: 🩺 Stability & Availability | 🟡 Minor | ⚡ Quick win

Remove the stale venv_tmp_dir class default to avoid an unsafe __del__ fallback.

venv_tmp_dir = 'venv_osc_dep_tmp' at Line 99 is a class attribute that __init__ always overwrites with tempfile.mkdtemp(prefix='fosslight_venv_') at Line 122. If super().__init__(...) (Line 106) or any statement before Line 122 raises, self.venv_tmp_dir falls back to this stale class attribute value. __del__ then runs shutil.rmtree(self.venv_tmp_dir, ignore_errors=True) at Line 128 against that relative path, resolved against the current working directory. Since the analysis chdirs into the target project (per the comment at Lines 110-114), this could remove a real directory in the user's project tree if it happens to be named venv_osc_dep_tmp.

Set the class default to None and guard __del__ (skip rmtree when the value is falsy or not the instance's own mkdtemp() path), or move the mkdtemp() call to the very first line of __init__.

🛡️ Proposed fix
-    venv_tmp_dir = 'venv_osc_dep_tmp'
+    venv_tmp_dir = None
     def __del__(self):
         if os.path.isfile(self.tmp_file_name):
             os.remove(self.tmp_file_name)

-        shutil.rmtree(self.venv_tmp_dir, ignore_errors=True)
+        if self.venv_tmp_dir:
+            shutil.rmtree(self.venv_tmp_dir, ignore_errors=True)
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/fosslight_dependency/package_manager/Pypi.py` around lines 95 - 132,
Change the Pypi class attribute venv_tmp_dir from the stale relative directory
name to None, and update __del__ to call shutil.rmtree only when the instance
has a valid mkdtemp-created directory path. Preserve cleanup of the temporary
files while ensuring initialization failures before the assignment cannot
trigger deletion of a project-relative directory.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Outside diff comments:
In `@src/fosslight_dependency/package_manager/Pypi.py`:
- Around line 95-132: Change the Pypi class attribute venv_tmp_dir from the
stale relative directory name to None, and update __del__ to call shutil.rmtree
only when the instance has a valid mkdtemp-created directory path. Preserve
cleanup of the temporary files while ensuring initialization failures before the
assignment cannot trigger deletion of a project-relative directory.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro Plus

Run ID: 69ad07c3-4634-4272-8ba9-76a4d8c07892

📥 Commits

Reviewing files that changed from the base of the PR and between 83c18c0 and c3b8f66.

📒 Files selected for processing (1)
  • src/fosslight_dependency/package_manager/Pypi.py

@bjk7119

bjk7119 commented Aug 1, 2026

Copy link
Copy Markdown
Contributor Author

@coderabbitai review

@coderabbitai

coderabbitai Bot commented Aug 1, 2026

Copy link
Copy Markdown
✅ Action performed

Review finished.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (2)
src/fosslight_dependency/package_manager/Pypi.py (2)

110-122: 🗄️ Data Integrity & Integration | 🔴 Critical | ⚡ Quick win

Prevent destructor cleanup from deleting the class-level fallback path.

If super().__init__() or tempfile.mkdtemp() raises, __del__() can resolve venv_tmp_dir = 'venv_osc_dep_tmp' from the class and pass that relative path to shutil.rmtree(). The failed construction can delete an unrelated directory in the current working directory. Initialize an instance sentinel before fallible calls, remove the class-level default, and guard cleanup to remove only a successfully created directory.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/fosslight_dependency/package_manager/Pypi.py` around lines 110 - 122,
Update the class containing venv_tmp_dir so it has no class-level fallback path;
initialize an instance sentinel before super().__init__() and tempfile.mkdtemp()
can raise, then have __del__() clean up only when that sentinel was replaced by
a successfully created temporary directory. Ensure failed construction cannot
pass a relative fallback path to shutil.rmtree().

298-327: 🔒 Security & Privacy | 🟠 Major | 🏗️ Heavy lift

Stop the command chain after virtualenv setup fails.

& and ; continue after a failing command. The shell returns the status of the last command, so describe_venv_failure() can accept a failed create_venv_cmd and then run pip install in the caller environment. The POSIX fallback also retries the complete install workflow for pip, network, or build failures instead of only virtualenv-creation failures. Run each step with argument-based subprocess calls and check=True, or use a short-circuiting chain. Retry only a creation-specific failure on a clean directory. (github.com)

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/fosslight_dependency/package_manager/Pypi.py` around lines 298 - 327,
Update the virtualenv setup flow that builds cmd_list and invokes subprocess.run
so each command is executed with argument-based calls and check=True, or through
a short-circuiting command chain, preventing later install steps from running
after creation fails. Restrict the POSIX fallback in the finally block to a
creation-specific failure on a clean directory, rather than retrying pip,
network, or build failures. Preserve describe_venv_failure and the existing
redacted error logging for final failures.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Outside diff comments:
In `@src/fosslight_dependency/package_manager/Pypi.py`:
- Around line 110-122: Update the class containing venv_tmp_dir so it has no
class-level fallback path; initialize an instance sentinel before
super().__init__() and tempfile.mkdtemp() can raise, then have __del__() clean
up only when that sentinel was replaced by a successfully created temporary
directory. Ensure failed construction cannot pass a relative fallback path to
shutil.rmtree().
- Around line 298-327: Update the virtualenv setup flow that builds cmd_list and
invokes subprocess.run so each command is executed with argument-based calls and
check=True, or through a short-circuiting command chain, preventing later
install steps from running after creation fails. Restrict the POSIX fallback in
the finally block to a creation-specific failure on a clean directory, rather
than retrying pip, network, or build failures. Preserve describe_venv_failure
and the existing redacted error logging for final failures.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro Plus

Run ID: 8aa0b1de-0545-4cbd-b297-4e687d5453ac

📥 Commits

Reviewing files that changed from the base of the PR and between c3b8f66 and e801c1f.

📒 Files selected for processing (1)
  • src/fosslight_dependency/package_manager/Pypi.py

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug [Issue] Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant