Skip to content

Commit

Permalink
[components] Fix uv error messages/warnings in dagster-dg tests (#2…
Browse files Browse the repository at this point in the history
…6443)

## Summary & Motivation

- `uv sync` was being incorrectly invoked using `uv run`
- Now all `uv` commands are run in an environment with `VIRTUAL_ENV`
unset, to get rid of the warnings about the uv-resolved environment not
matching the shell `VIRTUAL_ENV`

## How I Tested These Changes

Observed test output.
  • Loading branch information
smackesey authored Dec 12, 2024
1 parent 65479b8 commit bf7ea98
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
6 changes: 5 additions & 1 deletion python_modules/libraries/dagster-dg/dagster_dg/generate.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os
import subprocess
import textwrap
from pathlib import Path
from typing import Optional, Tuple
Expand All @@ -11,6 +12,8 @@
discover_git_root,
execute_code_location_command,
generate_subtree,
get_uv_command_env,
pushd,
)


Expand Down Expand Up @@ -63,7 +66,8 @@ def generate_code_location(path: Path, editable_dagster_root: Optional[str] = No
)

# Build the venv
execute_code_location_command(Path(path), ("uv", "sync"))
with pushd(path):
subprocess.run(["uv", "sync"], check=True, env=get_uv_command_env())


def generate_component_type(context: CodeLocationDirectoryContext, name: str) -> None:
Expand Down
13 changes: 11 additions & 2 deletions python_modules/libraries/dagster-dg/dagster_dg/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import re
import subprocess
from pathlib import Path
from typing import Any, Final, Iterator, List, Optional, Sequence, Union
from typing import Any, Final, Iterator, List, Mapping, Optional, Sequence, Union

import click
import jinja2
Expand All @@ -17,10 +17,19 @@
def execute_code_location_command(path: Path, cmd: Sequence[str]) -> str:
with pushd(path):
full_cmd = [*_CODE_LOCATION_COMMAND_PREFIX, *cmd]
result = subprocess.run(full_cmd, stdout=subprocess.PIPE, check=False)
result = subprocess.run(
full_cmd, stdout=subprocess.PIPE, env=get_uv_command_env(), check=True
)
return result.stdout.decode("utf-8")


# uv commands should be executed in an environment with no pre-existing VIRTUAL_ENV set. If this
# variable is set (common during development) and does not match the venv resolved by uv, it prints
# undesireable warnings.
def get_uv_command_env() -> Mapping[str, str]:
return {k: v for k, v in os.environ.items() if not k == "VIRTUAL_ENV"}


def discover_git_root(path: Path) -> Path:
while path != path.parent:
if (path / ".git").exists():
Expand Down

0 comments on commit bf7ea98

Please sign in to comment.