Skip to content

Commit

Permalink
Fix labels to work with canonical label literals. (bazelbuild#1700)
Browse files Browse the repository at this point in the history
For more details, see
https://docs.google.com/document/d/1N81qfCa8oskCk5LqTW-LNthy6EBrDot7bdUsjz6JFC4/edit#heading=h.5mcn15i0e1ch.

This allows us to use crate_universe on a repo with bzlmod enabled.
  • Loading branch information
matts1 authored Dec 15, 2022
1 parent 5826a50 commit 1469cd7
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions crate_universe/src/utils/starlark/label.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ impl FromStr for Label {
type Err = anyhow::Error;

fn from_str(s: &str) -> Result<Self, Self::Err> {
let re = Regex::new(r"^(@[\w\d\-_\.]*)?/{0,2}([\w\d\-_\./+]+)?(:([\+\w\d\-_\./]+))?$")?;
let re = Regex::new(r"^(@@?[\w\d\-_\.]*)?/{0,2}([\w\d\-_\./+]+)?(:([\+\w\d\-_\./]+))?$")?;
let cap = re
.captures(s)
.with_context(|| format!("Failed to parse label from string: {}", s))?;
Expand Down Expand Up @@ -192,6 +192,15 @@ mod test {
use std::fs::{create_dir_all, File};
use tempfile::tempdir;

#[test]
fn full_label_bzlmod() {
let label = Label::from_str("@@repo//package/sub_package:target").unwrap();
assert_eq!(label.to_string(), "@repo//package/sub_package:target");
assert_eq!(label.repository.unwrap(), "repo");
assert_eq!(label.package.unwrap(), "package/sub_package");
assert_eq!(label.target, "target");
}

#[test]
fn full_label() {
let label = Label::from_str("@repo//package/sub_package:target").unwrap();
Expand Down Expand Up @@ -276,8 +285,8 @@ mod test {
}

#[test]
fn invalid_double_at() {
assert!(Label::from_str("@@repo//pkg:target").is_err());
fn invalid_triple_at() {
assert!(Label::from_str("@@@repo//pkg:target").is_err());
}

#[test]
Expand Down

0 comments on commit 1469cd7

Please sign in to comment.