Skip to content

Commit 22d3622

Browse files
diliopfacebook-github-bot
authored andcommitted
support per version visibility
Summary: The `visibility` config within `fixups.toml` is only supported as a top level config or within `export_sources` so if one wants to modify visibility per crate version, there is currently no way to do that. I considered a couple of design approaches moving forward including making `version` a 1st class citizen by supporting `version` tables: ```toml [version."=2.2.1"] overlay = "some_overlay" visibility = ["//some/..."], [version."<2.2.1"] overlay = "other_overlay" visibility = ["//other/..."], ``` but the above needs a larger redesign we would have to move `version` out of per-platform configs. Since we are to move to a `fixups.bzl` implementation, I'd rather spent the time then and design the `fixups.bzl` spec from the ground up. For now, I went with the least intrusive approach by allowing the top level `visibility` to accept both TOML array and table values. Array values is what is currently supported: ```toml visibility = ["//some/..."] ``` and table values looks like so: ``` [visibility] "=2.2.1" = ["//some/..."] ">2.2.1" = ["//other/..."] ``` Reviewed By: dtolnay Differential Revision: D74600208 fbshipit-source-id: 5265517c87d16d1b7d17c18387b0bbed12531f5f
1 parent 7cd4a80 commit 22d3622

File tree

4 files changed

+34
-3
lines changed

4 files changed

+34
-3
lines changed

src/fixups.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ use crate::cargo::TargetKind;
4545
use crate::collection::SetOrMap;
4646
use crate::config::Config;
4747
use crate::config::VendorConfig;
48+
use crate::fixups::config::CustomVisibility;
4849
use crate::glob::Globs;
4950
use crate::glob::NO_EXCLUDE;
5051
use crate::glob::SerializableGlobSet as GlobSet;
@@ -188,8 +189,16 @@ impl<'meta> Fixups<'meta> {
188189
}
189190

190191
pub fn public_visibility(&self) -> Visibility {
191-
match self.fixup_config.custom_visibility.as_deref() {
192-
Some(visibility) => Visibility::Custom(visibility.to_vec()),
192+
match self.fixup_config.custom_visibility.as_ref() {
193+
Some(visibility) => match visibility {
194+
CustomVisibility::NoVersion(global) => Visibility::Custom(global.to_vec()),
195+
CustomVisibility::WithVersion(versioned) => versioned
196+
.iter()
197+
.filter(|(k, _)| k.matches(&self.package.version))
198+
.map(|(_, v)| Visibility::Custom(v.to_vec()))
199+
.next()
200+
.unwrap_or(Visibility::Public),
201+
},
193202
None => Visibility::Public,
194203
}
195204
}

src/fixups/config.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
use std::collections::BTreeMap;
99
use std::collections::BTreeSet;
10+
use std::collections::HashMap;
1011
use std::collections::HashSet;
1112
use std::fmt;
1213
use std::path::Path;
@@ -36,7 +37,7 @@ pub struct FixupConfigFile {
3637
/// This only has an effect for top-level crates. Exposed crates
3738
/// by default get `visibility = ["PUBLIC"]`. Sometimes you want to
3839
/// discourage use of some crate by limiting its visibility.
39-
pub custom_visibility: Option<Vec<String>>,
40+
pub custom_visibility: Option<CustomVisibility>,
4041

4142
/// Omit a target
4243
pub omit_targets: BTreeSet<String>,
@@ -314,6 +315,13 @@ impl<'de> Deserialize<'de> for CargoEnvs {
314315
}
315316
}
316317

318+
#[derive(Debug, Deserialize)]
319+
#[serde(untagged)]
320+
pub enum CustomVisibility {
321+
NoVersion(Vec<String>),
322+
WithVersion(HashMap<semver::VersionReq, Vec<String>>),
323+
}
324+
317325
struct FixupConfigFileVisitor;
318326

319327
impl<'de> Visitor<'de> for FixupConfigFileVisitor {

test/common/simple-two/Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
[package]
2+
name = "simple"
3+
version = "0.1.0"
4+
publish = false

test/common/simple-two/src/lib.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/*
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
pub fn simple() {
9+
println!("I am a simple crate")
10+
}

0 commit comments

Comments
 (0)