1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-21 15:04:11 -05:00

feat(config): Support frozen lockfile config option in deno.json (#25100)

Closes #24544
This commit is contained in:
Nathan Whitaker 2024-08-20 07:55:47 -07:00 committed by GitHub
parent f1c58ec041
commit 5168700be6
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 73 additions and 18 deletions

View file

@ -589,7 +589,7 @@ pub struct Flags {
pub argv: Vec<String>, pub argv: Vec<String>,
pub subcommand: DenoSubcommand, pub subcommand: DenoSubcommand,
pub frozen_lockfile: bool, pub frozen_lockfile: Option<bool>,
pub ca_stores: Option<Vec<String>>, pub ca_stores: Option<Vec<String>>,
pub ca_data: Option<CaData>, pub ca_data: Option<CaData>,
pub cache_blocklist: Vec<String>, pub cache_blocklist: Vec<String>,
@ -5231,7 +5231,7 @@ fn cached_only_arg_parse(flags: &mut Flags, matches: &mut ArgMatches) {
fn frozen_lockfile_arg_parse(flags: &mut Flags, matches: &mut ArgMatches) { fn frozen_lockfile_arg_parse(flags: &mut Flags, matches: &mut ArgMatches) {
if let Some(&v) = matches.get_one::<bool>("frozen") { if let Some(&v) = matches.get_one::<bool>("frozen") {
flags.frozen_lockfile = v; flags.frozen_lockfile = Some(v);
} }
} }
@ -10923,10 +10923,10 @@ mod tests {
#[test] #[test]
fn run_with_frozen_lockfile() { fn run_with_frozen_lockfile() {
let cases = [ let cases = [
(Some("--frozen"), true), (Some("--frozen"), Some(true)),
(Some("--frozen=true"), true), (Some("--frozen=true"), Some(true)),
(Some("--frozen=false"), false), (Some("--frozen=false"), Some(false)),
(None, false), (None, None),
]; ];
for (flag, frozen) in cases { for (flag, frozen) in cases {
let mut args = svec!["deno", "run"]; let mut args = svec!["deno", "run"];

View file

@ -147,22 +147,28 @@ impl CliLockfile {
}, },
}; };
let root_folder = workspace.root_folder_configs();
// CLI flag takes precedence over the config
let frozen = flags.frozen_lockfile.unwrap_or_else(|| {
root_folder
.deno_json
.as_ref()
.and_then(|c| c.to_lock_config().ok().flatten().map(|c| c.frozen()))
.unwrap_or(false)
});
let lockfile = if flags.lock_write { let lockfile = if flags.lock_write {
log::warn!( log::warn!(
"{} \"--lock-write\" flag is deprecated and will be removed in Deno 2.", "{} \"--lock-write\" flag is deprecated and will be removed in Deno 2.",
crate::colors::yellow("Warning") crate::colors::yellow("Warning")
); );
CliLockfile::new( CliLockfile::new(Lockfile::new_empty(filename, true), frozen)
Lockfile::new_empty(filename, true),
flags.frozen_lockfile,
)
} else { } else {
Self::read_from_path(filename, flags.frozen_lockfile)? Self::read_from_path(filename, frozen)?
}; };
// initialize the lockfile with the workspace's configuration // initialize the lockfile with the workspace's configuration
let root_url = workspace.root_dir(); let root_url = workspace.root_dir();
let root_folder = workspace.root_folder_configs();
let config = deno_lockfile::WorkspaceConfig { let config = deno_lockfile::WorkspaceConfig {
root: WorkspaceMemberConfig { root: WorkspaceMemberConfig {
package_json_deps: pkg_json_deps(root_folder.pkg_json.as_deref()), package_json_deps: pkg_json_deps(root_folder.pkg_json.as_deref()),

View file

@ -1846,7 +1846,12 @@ fn resolve_lockfile_from_workspace(
return None; return None;
} }
}; };
resolve_lockfile_from_path(lockfile_path) let frozen = workspace
.workspace
.root_deno_json()
.and_then(|c| c.to_lock_config().ok().flatten().map(|c| c.frozen()))
.unwrap_or(false);
resolve_lockfile_from_path(lockfile_path, frozen)
} }
fn resolve_node_modules_dir( fn resolve_node_modules_dir(
@ -1875,8 +1880,11 @@ fn resolve_node_modules_dir(
canonicalize_path_maybe_not_exists(&node_modules_dir).ok() canonicalize_path_maybe_not_exists(&node_modules_dir).ok()
} }
fn resolve_lockfile_from_path(lockfile_path: PathBuf) -> Option<CliLockfile> { fn resolve_lockfile_from_path(
match CliLockfile::read_from_path(lockfile_path, false) { lockfile_path: PathBuf,
frozen: bool,
) -> Option<CliLockfile> {
match CliLockfile::read_from_path(lockfile_path, frozen) {
Ok(value) => { Ok(value) => {
if value.filename.exists() { if value.filename.exists() {
if let Ok(specifier) = ModuleSpecifier::from_file_path(&value.filename) if let Ok(specifier) = ModuleSpecifier::from_file_path(&value.filename)

View file

@ -552,8 +552,20 @@
}, },
"lock": { "lock": {
"description": "Whether to use a lock file or the path to use for the lock file. Can be overridden by CLI arguments.", "description": "Whether to use a lock file or the path to use for the lock file. Can be overridden by CLI arguments.",
"type": ["string", "boolean"], "type": ["string", "boolean", "object"],
"default": true "default": true,
"properties": {
"path": {
"type": "string",
"description": "The path to use for the lock file.",
"default": "deno.lock"
},
"frozen": {
"type": "boolean",
"description": "Whether to exit with an error if lock file is out of date.",
"default": false
}
}
}, },
"unstable": { "unstable": {
"type": "array", "type": "array",

View file

@ -472,7 +472,7 @@ async fn resolve_shim_data(
executable_args.push("--cached-only".to_string()); executable_args.push("--cached-only".to_string());
} }
if flags.frozen_lockfile { if flags.frozen_lockfile.unwrap_or(false) {
executable_args.push("--frozen".to_string()); executable_args.push("--frozen".to_string());
} }

View file

@ -123,6 +123,35 @@
} }
] ]
}, },
"lockfile_config": {
"steps": [
{
"args": [
"eval",
"Deno.writeTextFileSync('deno.json', JSON.stringify({ lock: { frozen: true }, ...JSON.parse(Deno.readTextFileSync('deno.json')) }))"
],
"output": ""
},
{
"args": "cache --frozen=false add.ts",
"output": "[WILDCARD]"
},
{
// sub.ts imports from an npm package
// that's not in the lockfile
"args": "run sub.ts",
"output": "frozen_new_dep_run.out",
"exitCode": 1
},
{
"args": "cache sub.ts",
"output": "frozen_new_dep_cache.out",
"exitCode": 1
}
]
},
"non_analyzable_dynamic_npm": { "non_analyzable_dynamic_npm": {
"steps": [ "steps": [
{ {