Skip to content

Commit 5547478

Browse files
committed
fix(py_venv): External module imports repaired
1 parent d80279b commit 5547478

File tree

3 files changed

+41
-33
lines changed

3 files changed

+41
-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: 22 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -445,6 +445,7 @@ aspect-runfiles-repo = {1}
445445
/// the depth of the `ip` then back down to the workspace-relative path of
446446
/// the target file.
447447
pub fn populate_venv_with_copies(
448+
repo: &str,
448449
venv: Virtualenv,
449450
pth_file: PthFile,
450451
bin_dir: PathBuf,
@@ -455,7 +456,6 @@ pub fn populate_venv_with_copies(
455456

456457
// Get $PWD, which is the build working directory.
457458
let action_src_dir = current_dir().into_diagnostic()?;
458-
let main_module = action_src_dir.file_name().unwrap();
459459
let action_bin_dir = action_src_dir.join(bin_dir);
460460

461461
#[cfg(feature = "debug")]
@@ -483,7 +483,7 @@ pub fn populate_venv_with_copies(
483483
.into_diagnostic()?;
484484

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

489489
let line = line.trim().to_string();
@@ -496,12 +496,12 @@ pub fn populate_venv_with_copies(
496496
format!("{}/", line)
497497
};
498498

499-
let Some((workspace, entry_path)) = line.split_once("/") else {
499+
let Some((entry_repo, entry_path)) = line.split_once("/") else {
500500
return Err(miette!("Invalid path file entry!"));
501501
};
502502

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

506506
let mut entry = PathBuf::from(entry_path);
507507

@@ -511,9 +511,10 @@ pub fn populate_venv_with_copies(
511511
eprintln!("Entry is site-packages...");
512512

513513
// If the entry is external then we have to adjust the path
514-
if workspace != main_module {
514+
// FIXME: This isn't quite right outside of bzlmod
515+
if entry_repo != repo {
515516
entry = PathBuf::from("external")
516-
.join(PathBuf::from(workspace))
517+
.join(PathBuf::from(entry_repo))
517518
.join(entry)
518519
}
519520

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

561551
writeln!(dest_pth_writer, "# @{}", line).into_diagnostic()?;
562552
writeln!(
563553
dest_pth_writer,
564554
"{}",
565-
path_to_runfiles.join(entry).to_str().unwrap()
555+
path_to_runfiles // .runfiles
556+
.join(entry_repo) // ${REPO}
557+
.join(entry_path) // ${PATH}
558+
.to_str()
559+
.unwrap()
566560
)
567561
.into_diagnostic()?;
568562
}
@@ -579,6 +573,7 @@ pub fn populate_venv_with_copies(
579573
580574
#[expect(unused_variables)]
581575
pub fn populate_venv_with_pth(
576+
repo: &str,
582577
venv: Virtualenv,
583578
pth_file: PthFile,
584579
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)