Skip to content

Commit 8058f82

Browse files
committed
fix(py_venv): External module imports repaired
1 parent 5ec374e commit 8058f82

File tree

3 files changed

+40
-33
lines changed

3 files changed

+40
-33
lines changed

WORKSPACE

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,3 +317,8 @@ oci_pull(
317317
load("@container_structure_test//:repositories.bzl", "container_structure_test_register_toolchain")
318318

319319
container_structure_test_register_toolchain(name = "cst")
320+
321+
local_repository(
322+
name = "rpy610_test",
323+
path = "./py/tests/rpy610/subrepo",
324+
)

py/tools/py/src/venv.rs

Lines changed: 21 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -448,6 +448,7 @@ aspect-runfiles-repo = {1}
448448
/// the depth of the `ip` then back down to the workspace-relative path of
449449
/// the target file.
450450
pub fn populate_venv_with_copies(
451+
repo: &str,
451452
venv: Virtualenv,
452453
pth_file: PthFile,
453454
bin_dir: PathBuf,
@@ -458,7 +459,6 @@ pub fn populate_venv_with_copies(
458459

459460
// Get $PWD, which is the build working directory.
460461
let action_src_dir = current_dir().into_diagnostic()?;
461-
let main_module = action_src_dir.file_name().unwrap();
462462
let action_bin_dir = action_src_dir.join(bin_dir);
463463

464464
#[cfg(feature = "debug")]
@@ -486,7 +486,7 @@ pub fn populate_venv_with_copies(
486486
.into_diagnostic()?;
487487

488488
for line in BufReader::new(source_pth).lines().map_while(Result::ok) {
489-
//#[cfg(feature = "debug")]
489+
#[cfg(feature = "debug")]
490490
eprintln!("Got pth line {}", &line);
491491

492492
let line = line.trim().to_string();
@@ -499,12 +499,12 @@ pub fn populate_venv_with_copies(
499499
format!("{}/", line)
500500
};
501501

502-
let Some((workspace, entry_path)) = line.split_once("/") else {
502+
let Some((entry_repo, entry_path)) = line.split_once("/") else {
503503
return Err(miette!("Invalid path file entry!"));
504504
};
505505

506506
#[cfg(feature = "debug")]
507-
eprintln!("Got pth entry @{}//{}", workspace, entry_path);
507+
eprintln!("Got pth entry @{}//{}", entry_repo, entry_path);
508508

509509
let mut entry = PathBuf::from(entry_path);
510510

@@ -515,9 +515,9 @@ pub fn populate_venv_with_copies(
515515

516516
// If the entry is external then we have to adjust the path
517517
// FIXME: This isn't quite right outside of bzlmod
518-
if workspace != main_module {
518+
if entry_repo != repo {
519519
entry = PathBuf::from("external")
520-
.join(PathBuf::from(workspace))
520+
.join(PathBuf::from(entry_repo))
521521
.join(entry)
522522
}
523523

@@ -542,32 +542,25 @@ pub fn populate_venv_with_copies(
542542
}
543543
}
544544
} else {
545-
// Need to insert an appropriate pth file entry. Pth file lines
546-
// are relativized to the site dir [1] so here we need to take
547-
// the path from the site dir back to the root of the runfiles
548-
// tree and then append the entry to that relative path.
549-
//
550-
// This is the path from the venv's site-packages destination
551-
// "back up to" the bazel-bin dir we're building into, plus one
552-
// level.
553-
//
554-
// [1] https://github.com/python/cpython/blob/ce31ae5209c976d28d1c21fcbb06c0ae5e50a896/Lib/site.py#L215
555-
556-
// aspect-build/rules_py#610
557-
//
558-
// While these relative paths seem to work fine for _internal_
559-
// runfiles within the `_main` workspace, problems occur when we
560-
// try to take relative paths to _other_ workspaces because bzlmod
561-
// may munge the directory names to be something that doesn't
562-
// exist.
563-
let path_to_runfiles =
564-
diff_paths(&action_bin_dir, action_bin_dir.join(&venv.site_dir)).unwrap();
545+
if entry_repo != repo {
546+
eprintln!("Warning: @@{entry_repo}//{entry_path} is not `site-packages`via pth rather than copy",)
547+
}
548+
// The path to the runfiles root is _one more than_ the relative
549+
// oath from the venv's target dir to the root of the module
550+
// containing the venv.
551+
let path_to_runfiles = diff_paths(&action_bin_dir, action_bin_dir.join(&venv.site_dir))
552+
.unwrap()
553+
.join("..");
565554

566555
writeln!(dest_pth_writer, "# @{}", line).into_diagnostic()?;
567556
writeln!(
568557
dest_pth_writer,
569558
"{}",
570-
path_to_runfiles.join(entry).to_str().unwrap()
559+
path_to_runfiles // .runfiles
560+
.join(entry_repo) // ${REPO}
561+
.join(entry_path) // ${PATH}
562+
.to_str()
563+
.unwrap()
571564
)
572565
.into_diagnostic()?;
573566
}
@@ -584,6 +577,7 @@ pub fn populate_venv_with_copies(
584577
585578
#[expect(unused_variables)]
586579
pub fn populate_venv_with_pth(
580+
repo: &str,
587581
venv: Virtualenv,
588582
pth_file: PthFile,
589583
bin_dir: PathBuf,

py/tools/venv_bin/src/main.rs

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -130,10 +130,13 @@ fn venv_cmd_handler(args: VenvArgs) -> miette::Result<()> {
130130
return Err(miette!("Version must be provided for static venv modes"));
131131
};
132132

133+
let repo = args
134+
.repo
135+
.as_deref()
136+
.expect("The --repo argument is required for static venvs!");
137+
133138
let venv = py::venv::create_empty_venv(
134-
args.repo
135-
.as_deref()
136-
.expect("The --repo argument is required for static venvs!"),
139+
repo,
137140
&args.python,
138141
py::venv::PythonVersionInfo::from_str(&version)?,
139142
&args.location,
@@ -145,6 +148,7 @@ fn venv_cmd_handler(args: VenvArgs) -> miette::Result<()> {
145148
)?;
146149

147150
py::venv::populate_venv_with_copies(
151+
repo,
148152
venv,
149153
pth_file,
150154
args.bin_dir.unwrap(),
@@ -160,10 +164,13 @@ fn venv_cmd_handler(args: VenvArgs) -> miette::Result<()> {
160164
return Err(miette!("Version must be provided for static venv modes"));
161165
};
162166

167+
let repo = args
168+
.repo
169+
.as_deref()
170+
.expect("The --repo argument is required for static venvs!");
171+
163172
let venv = py::venv::create_empty_venv(
164-
args.repo
165-
.as_deref()
166-
.expect("The --repo argument is required for static venvs!"),
173+
repo,
167174
&args.python,
168175
py::venv::PythonVersionInfo::from_str(&version)?,
169176
&args.location,
@@ -175,6 +182,7 @@ fn venv_cmd_handler(args: VenvArgs) -> miette::Result<()> {
175182
)?;
176183

177184
py::venv::populate_venv_with_pth(
185+
repo,
178186
venv,
179187
pth_file,
180188
args.bin_dir.unwrap(),

0 commit comments

Comments
 (0)