Skip to content

Commit e436b76

Browse files
Allow first-party builds with no-build (#21294)
With `no-build = true` configured, uv rejects explicit `uv build` targets and first-party packages installed non-editably by `uv sync`, including projects in the workspace. Build restrictions should apply to third-party dependencies instead. Here we allow explicit builds and carry workspace-member identity through locked directory distributions so first-party packages can be prepared and built. Dependency resolution and installation continue to honor global and package-specific build restrictions, including for path dependencies outside the workspace. Follow-up to astral-sh/ty#4384. --------- Co-authored-by: zaniebot <[email protected]>
1 parent ba1f8c1 commit e436b76

17 files changed

Lines changed: 235 additions & 77 deletions

File tree

‎crates/uv-build-frontend/src/error.rs‎

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,6 @@ pub enum Error {
4949
MissingHeader(#[from] Box<MissingHeaderError>),
5050
#[error("Failed to build PATH for build script")]
5151
BuildScriptPath(#[source] env::JoinPathsError),
52-
// For the convenience of typing `setup_build` properly.
53-
#[error("Building source distributions for `{0}` is disabled")]
54-
NoSourceDistBuild(PackageName),
55-
#[error("Building source distributions is disabled")]
56-
NoSourceDistBuilds,
5752
#[error("Cyclic build dependency detected for `{0}`")]
5853
CyclicBuildDependency(PackageName),
5954
#[error(
@@ -75,8 +70,6 @@ impl IsBuildBackendError for Error {
7570
| Self::RequirementsResolve(_, _)
7671
| Self::RequirementsInstall(_, _)
7772
| Self::Virtualenv(_)
78-
| Self::NoSourceDistBuild(_)
79-
| Self::NoSourceDistBuilds
8073
| Self::CyclicBuildDependency(_)
8174
| Self::UnmatchedRuntime(_, _) => false,
8275
Self::CommandFailed(_, _)

‎crates/uv-cli/src/lib.rs‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7158,8 +7158,9 @@ pub struct BuildOptionsArgs {
71587158
/// Don't build source distributions.
71597159
///
71607160
/// When enabled, uv will reuse cached wheels from previously built source distributions, but
7161-
/// operations that require building a source distribution will exit with an error. uv may
7162-
/// still build editable requirements, and their build backends may run arbitrary Python code.
7161+
/// operations that require building a source distribution will exit with an error. First-party
7162+
/// packages, such as projects in the workspace, will still be built. uv will also still build
7163+
/// editable requirements, and their build backends may run arbitrary Python code.
71637164
#[arg(
71647165
long,
71657166
env = EnvVars::UV_NO_BUILD,
@@ -7178,6 +7179,8 @@ pub struct BuildOptionsArgs {
71787179
build: bool,
71797180

71807181
/// Don't build source distributions for a specific package [env: `UV_NO_BUILD_PACKAGE`=]
7182+
///
7183+
/// First-party packages, such as projects in the workspace, will still be built.
71817184
#[arg(
71827185
long,
71837186
help_heading = "Build options",

‎crates/uv-dispatch/src/lib.rs‎

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -510,22 +510,6 @@ impl BuildContext for BuildDispatch<'_> {
510510
VersionOrUrlRef::Url(_) => None,
511511
});
512512

513-
// Note we can only prevent builds by name for packages with names
514-
// unless all builds are disabled.
515-
if self
516-
.build_options
517-
.no_build_requirement(dist_name)
518-
// We always allow editable builds
519-
&& !matches!(build_kind, BuildKind::Editable)
520-
{
521-
let err = if let Some(dist) = dist {
522-
uv_build_frontend::Error::NoSourceDistBuild(dist.name().clone())
523-
} else {
524-
uv_build_frontend::Error::NoSourceDistBuilds
525-
};
526-
return Err(err);
527-
}
528-
529513
// Push the current distribution onto the build stack, to prevent cyclic dependencies.
530514
if let Some(dist) = dist {
531515
build_stack.insert(dist.distribution_id());

‎crates/uv-distribution-types/src/buildable.rs‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,14 @@ impl BuildableSource<'_> {
6969
}
7070
}
7171

72+
/// Returns `true` if the source is a first-party workspace member.
73+
pub fn is_first_party(&self) -> bool {
74+
match self {
75+
Self::Dist(dist) => dist.is_first_party(),
76+
Self::Url(_) => false,
77+
}
78+
}
79+
7280
/// Return true if the source refers to a local source tree (i.e., a directory).
7381
pub fn is_source_tree(&self) -> bool {
7482
match self {

‎crates/uv-distribution-types/src/lib.rs‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,13 @@ pub struct PathSourceDist {
383383
pub url: VerbatimUrl,
384384
}
385385

386+
/// Whether a source distribution is a first-party workspace member.
387+
#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq)]
388+
pub enum FirstParty {
389+
Yes,
390+
No,
391+
}
392+
386393
/// A source distribution that exists in a local directory.
387394
#[derive(Debug, Clone, Hash, PartialEq, Eq)]
388395
pub struct DirectorySourceDist {
@@ -393,6 +400,8 @@ pub struct DirectorySourceDist {
393400
pub editable: Option<bool>,
394401
/// Whether the package should be built and installed.
395402
pub r#virtual: Option<bool>,
403+
/// Whether the package is a first-party workspace member.
404+
pub first_party: FirstParty,
396405
/// The URL as it was provided by the user.
397406
pub url: VerbatimUrl,
398407
}
@@ -532,6 +541,7 @@ impl Dist {
532541
install_path: install_path.into_boxed_path(),
533542
editable,
534543
r#virtual,
544+
first_party: FirstParty::No,
535545
url,
536546
})))
537547
}
@@ -782,6 +792,25 @@ impl SourceDist {
782792
}
783793
}
784794

795+
/// Returns `true` if the distribution is a first-party workspace member.
796+
pub fn is_first_party(&self) -> bool {
797+
match self {
798+
Self::Directory(DirectorySourceDist {
799+
first_party: FirstParty::Yes,
800+
..
801+
}) => true,
802+
Self::Directory(DirectorySourceDist {
803+
first_party: FirstParty::No,
804+
..
805+
})
806+
| Self::Registry(_)
807+
| Self::DirectUrl(_)
808+
| Self::GitDirectory(_)
809+
| Self::GitPath(_)
810+
| Self::Path(_) => false,
811+
}
812+
}
813+
785814
/// Returns `true` if the distribution refers to a local file or directory.
786815
fn is_local(&self) -> bool {
787816
matches!(self, Self::Directory(_) | Self::Path(_))

‎crates/uv-distribution/src/source/mod.rs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2889,8 +2889,8 @@ impl<'a, T: BuildContext> SourceDistributionBuilder<'a, T> {
28892889
.build_options()
28902890
.no_build_requirement(source.name())
28912891
{
2892-
if source.is_editable() {
2893-
debug!("Allowing build for editable source distribution: {source}");
2892+
if source.is_editable() || source.is_first_party() {
2893+
debug!("Allowing build for first-party or editable source distribution: {source}");
28942894
} else {
28952895
return Err(Error::NoBuild);
28962896
}

‎crates/uv-installer/src/preparer.rs‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,10 @@ impl<'a, Context: BuildContext> Preparer<'a, Context> {
123123
}
124124
Dist::Source(ref dist) => {
125125
if self.build_options.no_build_package(dist.name()) {
126-
if dist.is_editable() {
127-
debug!("Allowing build for editable source distribution: {dist}");
126+
if dist.is_editable() || dist.is_first_party() {
127+
debug!(
128+
"Allowing build for first-party or editable source distribution: {dist}"
129+
);
128130
} else {
129131
return Err(Error::NoBuild(dist.name().clone()));
130132
}

‎crates/uv-resolver/src/lock/export/pylock_toml.rs‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@ use uv_distribution_filename::{
2323
};
2424
use uv_distribution_types::{
2525
BuiltDist, DirectUrlBuiltDist, DirectUrlSourceDist, DirectorySourceDist, Dist, Edge,
26-
FileLocation, GitDirectorySourceDist, IndexUrl, Name, Node, PathBuiltDist, PathSourceDist,
27-
RegistryBuiltDist, RegistryBuiltWheel, RegistrySourceDist, RemoteSource, RequiresPython,
28-
Resolution, ResolvedDist, SourceDist, ToUrlError, UrlString,
26+
FileLocation, FirstParty, GitDirectorySourceDist, IndexUrl, Name, Node, PathBuiltDist,
27+
PathSourceDist, RegistryBuiltDist, RegistryBuiltWheel, RegistrySourceDist, RemoteSource,
28+
RequiresPython, Resolution, ResolvedDist, SourceDist, ToUrlError, UrlString,
2929
};
3030
use uv_fs::{PortablePathBuf, normalize_path, try_relative_to_if};
3131
use uv_git::{RepositoryReference, ResolvedRepositoryReference};
@@ -825,7 +825,7 @@ impl<'lock> PylockToml {
825825
};
826826

827827
// Extract the source distribution from the lockfile entry.
828-
let sdist = package.to_source_dist(target.install_path())?;
828+
let sdist = package.to_source_dist(target.install_path(), FirstParty::No)?;
829829

830830
// Extract some common fields from the source distribution.
831831
let size = package
@@ -1542,6 +1542,7 @@ impl PylockTomlDirectory {
15421542
install_path: path.into_owned().into_boxed_path(),
15431543
editable: self.editable,
15441544
r#virtual: Some(false),
1545+
first_party: FirstParty::No,
15451546
url,
15461547
})
15471548
}

‎crates/uv-resolver/src/lock/installable.rs‎

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use uv_configuration::{
1313
BuildOptions, DependencyGroupsWithDefaults, ExtrasSpecification,
1414
ExtrasSpecificationWithDefaults, InstallOptions,
1515
};
16-
use uv_distribution_types::{Edge, Node, Resolution, ResolvedDist};
16+
use uv_distribution_types::{Edge, FirstParty, Node, Resolution, ResolvedDist};
1717
use uv_normalize::{DefaultExtras, ExtraName, GroupName, PackageName};
1818
use uv_platform_tags::Tags;
1919
use uv_pypi_types::{ConflictKind, ConflictSet, ResolverMarkerEnvironment};
@@ -173,8 +173,17 @@ pub trait Installable<'lock> {
173173
build_options: &BuildOptions,
174174
) -> Result<Node, LockError> {
175175
let tag_policy = TagPolicy::Required(tags);
176-
let HashedDist { dist, hashes } =
177-
package.to_dist(self.install_path(), tag_policy, build_options, marker_env)?;
176+
let HashedDist { dist, hashes } = package.to_dist(
177+
self.install_path(),
178+
tag_policy,
179+
build_options,
180+
marker_env,
181+
if self.lock().is_workspace_member(package) {
182+
FirstParty::Yes
183+
} else {
184+
FirstParty::No
185+
},
186+
)?;
178187
let version = package.version().cloned();
179188
let dist = ResolvedDist::Installable {
180189
dist: Arc::new(dist),
@@ -199,6 +208,7 @@ pub trait Installable<'lock> {
199208
TagPolicy::Preferred(tags),
200209
&BuildOptions::default(),
201210
marker_env,
211+
FirstParty::No,
202212
)?;
203213
let version = package.version().cloned();
204214
let dist = ResolvedDist::Installable {

‎crates/uv-resolver/src/lock/mod.rs‎

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,11 @@ use uv_distribution_filename::{
3232
};
3333
use uv_distribution_types::{
3434
BuiltDist, DependencyMetadata, DirectUrlBuiltDist, DirectUrlSourceDist, DirectorySourceDist,
35-
Dist, FileLocation, GitDirectorySourceDist, GitPathBuiltDist, GitPathSourceDist, Identifier,
36-
IndexLocations, IndexMetadata, IndexUrl, Name, PYPI_URL, PathBuiltDist, PathSourceDist,
37-
RegistryBuiltDist, RegistryBuiltWheel, RegistrySourceDist, RemoteSource, Requirement,
38-
RequirementSource, RequiresPython, ResolvedDist, SimplifiedMarkerTree, StaticMetadata,
39-
ToUrlError, UrlString,
35+
Dist, FileLocation, FirstParty, GitDirectorySourceDist, GitPathBuiltDist, GitPathSourceDist,
36+
Identifier, IndexLocations, IndexMetadata, IndexUrl, Name, PYPI_URL, PathBuiltDist,
37+
PathSourceDist, RegistryBuiltDist, RegistryBuiltWheel, RegistrySourceDist, RemoteSource,
38+
Requirement, RequirementSource, RequiresPython, ResolvedDist, SimplifiedMarkerTree,
39+
StaticMetadata, ToUrlError, UrlString,
4040
};
4141
use uv_fs::{PortablePath, PortablePathBuf, Simplified, normalize_path, try_relative_to_if};
4242
use uv_git::{RepositoryReference, ResolvedRepositoryReference};
@@ -1436,6 +1436,12 @@ impl Lock {
14361436
&self.manifest.members
14371437
}
14381438

1439+
/// Returns `true` if the package is a workspace member.
1440+
fn is_workspace_member(&self, package: &Package) -> bool {
1441+
self.members().contains(&package.id.name)
1442+
|| self.members().is_empty() && self.root().is_some_and(|root| root.id == package.id)
1443+
}
1444+
14391445
/// Returns the root requirements that were used to generate this lock.
14401446
fn requirements(&self) -> &BTreeSet<Requirement> {
14411447
&self.manifest.requirements
@@ -3181,8 +3187,13 @@ impl Lock {
31813187
index: &InMemoryIndex,
31823188
database: &DistributionDatabase<'_, Context>,
31833189
) -> Result<DistributionMetadata, LockError> {
3184-
let HashedDist { dist, .. } =
3185-
package.to_dist(root, TagPolicy::Preferred(tags), build_options, markers)?;
3190+
let HashedDist { dist, .. } = package.to_dist(
3191+
root,
3192+
TagPolicy::Preferred(tags),
3193+
build_options,
3194+
markers,
3195+
FirstParty::No,
3196+
)?;
31863197
let id = dist.distribution_id();
31873198
if let Some(archive) = index
31883199
.distributions()
@@ -3865,6 +3876,7 @@ impl Package {
38653876
tag_policy: TagPolicy<'_>,
38663877
build_options: &BuildOptions,
38673878
markers: &MarkerEnvironment,
3879+
first_party: FirstParty,
38683880
) -> Result<HashedDist, LockError> {
38693881
let no_binary = build_options.no_binary_package(&self.id.name);
38703882
let no_build = build_options.no_build_package(&self.id.name);
@@ -3995,11 +4007,11 @@ impl Package {
39954007
}
39964008
}
39974009

3998-
if let Some(sdist) = self.to_source_dist(workspace_root)? {
3999-
// Even with `--no-build`, allow virtual packages. (In the future, we may want to allow
4000-
// any local source tree, or at least editable source trees, which we allow in
4001-
// `uv pip`.)
4002-
if !no_build || sdist.is_virtual() {
4010+
if let Some(sdist) = self.to_source_dist(workspace_root, first_party)? {
4011+
// Even with `--no-build`, allow virtual packages and first-party workspace members. In
4012+
// the future, we may want to allow any local source tree, or at least editable source
4013+
// trees, as we do in `uv pip`.
4014+
if !no_build || sdist.is_virtual() || sdist.is_first_party() {
40034015
let hashes = self
40044016
.sdist
40054017
.as_ref()
@@ -4072,6 +4084,7 @@ impl Package {
40724084
fn to_source_dist(
40734085
&self,
40744086
workspace_root: &Path,
4087+
first_party: FirstParty,
40754088
) -> Result<Option<uv_distribution_types::SourceDist>, LockError> {
40764089
let sdist = match &self.id.source {
40774090
Source::Path(path) => {
@@ -4111,6 +4124,7 @@ impl Package {
41114124
install_path: install_path.into_boxed_path(),
41124125
editable: Some(false),
41134126
r#virtual: Some(false),
4127+
first_party,
41144128
};
41154129
uv_distribution_types::SourceDist::Directory(dir_dist)
41164130
}
@@ -4123,6 +4137,7 @@ impl Package {
41234137
install_path: install_path.into_boxed_path(),
41244138
editable: Some(true),
41254139
r#virtual: Some(false),
4140+
first_party,
41264141
};
41274142
uv_distribution_types::SourceDist::Directory(dir_dist)
41284143
}
@@ -4135,6 +4150,7 @@ impl Package {
41354150
install_path: install_path.into_boxed_path(),
41364151
editable: Some(false),
41374152
r#virtual: Some(true),
4153+
first_party,
41384154
};
41394155
uv_distribution_types::SourceDist::Directory(dir_dist)
41404156
}

0 commit comments

Comments
 (0)