Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add git info to build #4804

Merged
merged 15 commits into from
Aug 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .github/workflows/test-rust.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ jobs:
steps:
- name: 📂 Checkout code
uses: actions/checkout@v4
with:
fetch-tags: true
- name: Run docker compose
# This can go early because the DBs take a few seconds to start up.
if: ${{ contains(inputs.features, 'test-dbs-external') }}
Expand Down
2 changes: 2 additions & 0 deletions .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ jobs:
steps:
- name: 📂 Checkout code
uses: actions/checkout@v4
with:
fetch-tags: true
- uses: dorny/paths-filter@v3
id: changes
with:
Expand Down
100 changes: 98 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ consolidate-commits = true
[workspace.dependencies]
anyhow = "1.0.86"
enum-as-inner = "0.6.0"
insta = {version = "1.39.0", features = ["colors", "glob", "yaml"]}
insta = {version = "1.39.0", features = ["colors", "glob", "yaml", "filters"]}
insta-cmd = "0.6.0"
itertools = "0.13.0"
log = "0.4.22"
Expand Down
9 changes: 7 additions & 2 deletions prqlc/prqlc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ rust-version.workspace = true
version.workspace = true

# Required for `cargo-msrv`, which doesn't yet support workspaces
metadata.msrv = "1.70.0"
metadata.msrv = "1.73.0"

build = "build.rs"

[features]
cli = [
Expand Down Expand Up @@ -38,7 +40,7 @@ test-dbs = ["anyhow", "duckdb", "rusqlite", "tokio"]
test-dbs-external = ["mysql", "pg_bigdecimal", "postgres", "test-dbs", "tiberius", "tokio-util"]

[dependencies]
prqlc-parser = {path = "../prqlc-parser", version = "0.13.1" }
prqlc-parser = {path = "../prqlc-parser", version = "0.13.1"}

anstream = {version = "0.6.15", features = ["auto"]}
ariadne = "0.4.1"
Expand All @@ -60,6 +62,9 @@ sqlparser = {version = "0.49.0", features = ["serde"]}
strum = {version = "0.26.3", features = ["std", "derive"]}
strum_macros = "0.26.4"

[build-dependencies]
vergen-gitcl = {version = "1.0.0", features = ["build"]}

[target.'cfg(not(target_family="wasm"))'.dependencies]

# unique dependencies from the CLI, marked as optional and included in the 'cli'
Expand Down
11 changes: 11 additions & 0 deletions prqlc/prqlc/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
use std::error::Error;
// gix failing on https://github.com/rustyhorde/vergen/issues/359, and `git2`
// fails on `aarch64` so we're using `gitcl`. Switch to `gitx` when that bug is
// fixed.
use vergen_gitcl::{Emitter, GitclBuilder as GitBuilder};

pub fn main() -> Result<(), Box<dyn Error>> {
let git = GitBuilder::default().describe(true, true, None).build()?;
Emitter::default().add_instructions(&git)?.emit()?;
Ok(())
}
2 changes: 2 additions & 0 deletions prqlc/prqlc/src/cli/docs_generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,8 @@ mod tests {

#[test]
fn generate_html_docs() {
std::env::set_var("PRQL_VERSION_OVERRIDE", env!("CARGO_PKG_VERSION"));

let input = r"
#! This is the x function.
let x = arg1 arg2 -> c
Expand Down
11 changes: 10 additions & 1 deletion prqlc/prqlc/src/cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ use is_terminal::IsTerminal;
use itertools::Itertools;
use schemars::schema_for;

use prqlc::compiler_version;
use prqlc::debug;
use prqlc::internal::pl_to_lineage;
use prqlc::ir::{pl, rq};
Expand Down Expand Up @@ -86,8 +87,16 @@ struct Cli {
verbose: clap_verbosity_flag::Verbosity<LoggingHelp>,
}

/// This seems to be required because passing `compiler_version()` directly to
/// `command` fails because it's not a string, and we can't seem to convert it
/// to a string inline.
pub fn compiler_version_str() -> &'static str {
static COMPILER_VERSION: std::sync::OnceLock<String> = std::sync::OnceLock::new();
COMPILER_VERSION.get_or_init(|| compiler_version().to_string())
}

#[derive(Subcommand, Debug, Clone)]
#[command(name = env!("CARGO_PKG_NAME"), about, version)]
#[command(name = env!("CARGO_PKG_NAME"), about, version=compiler_version_str())]
enum Command {
/// Parse into PL AST
Parse {
Expand Down
28 changes: 27 additions & 1 deletion prqlc/prqlc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,10 +131,36 @@

pub type Result<T, E = Error> = core::result::Result<T, E>;

/// Get the version of the compiler. This is determined by the first of:
/// - An optional environment variable `PRQL_VERSION_OVERRIDE`. Note that this
/// needs to be set before the first time this function is called, since it's
/// stored in a static. It's primarily useful for testing.
/// - The version returned by `git describe --tags`
/// - The version in the cargo manifest

Check warning on line 139 in prqlc/prqlc/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

prqlc/prqlc/src/lib.rs#L137-L139

Added lines #L137 - L139 were not covered by tests
pub fn compiler_version() -> &'static Version {
static COMPILER_VERSION: OnceLock<Version> = OnceLock::new();
COMPILER_VERSION.get_or_init(|| {
Version::parse(env!("CARGO_PKG_VERSION")).expect("Invalid prqlc version number")
if let Ok(prql_version_override) = std::env::var("PRQL_VERSION_OVERRIDE") {
return Version::parse(&prql_version_override).unwrap_or_else(|e| {
panic!(
"Could not parse PRQL version {}\n{}",
prql_version_override, e
)
});
}
let git_version = env!("VERGEN_GIT_DESCRIBE");
let cargo_version = env!("CARGO_PKG_VERSION");
Version::parse(git_version)
.or_else(|e| {

Check warning on line 154 in prqlc/prqlc/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

prqlc/prqlc/src/lib.rs#L151-L154

Added lines #L151 - L154 were not covered by tests
log::info!("Could not parse git version number {}\n{}", git_version, e);
Version::parse(cargo_version)
})
.unwrap_or_else(|e| {
panic!(
"Could not parse prqlc version number {}\n{}",
cargo_version, e
)
})
})
}

Expand Down
17 changes: 15 additions & 2 deletions prqlc/prqlc/src/semantic/lowering.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use enum_as_inner::EnumAsInner;
use itertools::Itertools;
use prqlc_parser::generic::{InterpolateItem, Range, SwitchCase};
use prqlc_parser::lexer::lr::Literal;
use semver::{Prerelease, Version};

use crate::compiler_version;
use crate::ir::decl::{self, DeclKind, Module, RootModule, TableExpr};
Expand Down Expand Up @@ -125,8 +126,20 @@ fn tuple_fields_to_relation_columns(columns: Vec<TyTupleField>) -> Vec<RelationC

fn validate_query_def(query_def: &QueryDef) -> Result<()> {
if let Some(requirement) = &query_def.version {
if !requirement.matches(compiler_version()) {
return Err(Error::new_simple("This query uses a version of PRQL that is not supported by prqlc. Please upgrade the compiler."));
let current_version = compiler_version();

// We need to remove the pre-release part of the version, because
// otherwise those will fail the match.
let clean_version = Version {
pre: Prerelease::EMPTY,
..current_version.clone()
};

if !requirement.matches(&clean_version) {
return Err(Error::new_simple(format!(
"This query requires version {} of PRQL that is not supported by prqlc version {} (shortened from {}). Please upgrade the compiler.",
requirement, clean_version, current_version
)));
}
}
Ok(())
Expand Down
45 changes: 23 additions & 22 deletions prqlc/prqlc/tests/integration/sql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4446,31 +4446,32 @@ fn test_header_target_error() {
}

#[test]
fn prql_version() {
assert_snapshot!(compile(r#"
fn shortest_prql_version() {
let mut escape_version = insta::Settings::new();
escape_version.add_filter(r"'.*'", "[VERSION]");
escape_version.bind(|| {
assert_snapshot!(compile(r#"[{version = prql.version}]"#).unwrap(),@r###"
WITH table_0 AS (
SELECT
[VERSION] AS version
)
SELECT
version
FROM
table_0
"###);

assert_snapshot!(compile(r#"
from x
derive y = std.prql.version
"#).unwrap(),@r###"
SELECT
*,
'0.13.1' AS y
FROM
x
"###);
}

#[test]
fn shortest_prql_version() {
assert_snapshot!(compile(r#"[{version = prql.version}]"#).unwrap(),@r###"
WITH table_0 AS (
SELECT
'0.13.1' AS version
)
SELECT
version
FROM
table_0
"###);
SELECT
*,
[VERSION] AS y
FROM
x
"###);
})
}

#[test]
Expand Down
2 changes: 1 addition & 1 deletion web/book/src/project/target.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ echo 'prql target:sql.generic
PRQL allows specifying a version of the language in the PRQL header, like:

```prql
prql version:"0.13.1"
prql version:"0.13.0"

from employees
```
Expand Down
Loading
Loading