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

fix(cli): Warn on not-run lifecycle scripts with global cache #25786

Merged
merged 18 commits into from
Sep 24, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Small cleanup
  • Loading branch information
nathanwhit committed Aug 9, 2024
commit acca1ca4e8ef50f95790792289540985abda58c5
20 changes: 11 additions & 9 deletions cli/npm/managed/resolvers/common.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.

pub(super) mod bin_entries;
pub mod bin_entries;

use std::borrow::Cow;
use std::collections::HashMap;
Expand Down Expand Up @@ -209,20 +209,25 @@ impl<'a> LifecycleScripts<'a> {
PackagesAllowedScripts::None => false,
}
}
/// Register a package for running lifecycle scripts, if applicable.
///
/// `package_path` is the path containing the package's code (its root dir).
/// `package_meta_path` is the path to serve as the base directory for lifecycle
/// script-related metadata (e.g. to store whether the scripts have been run already)
pub fn add(
&mut self,
package: &NpmResolutionPackage,
package_path: &Path,
package_path: Cow<Path>,
package_meta_path: &Path,
) {
if has_lifecycle_scripts(package, package_path) {
if has_lifecycle_scripts(package, &package_path) {
let scripts_run = package_meta_path.join(".scripts-run");
let has_warned = package_meta_path.join(".scripts-warned");
if self.can_run_scripts(&package.id.nv) {
if !scripts_run.exists() {
self.packages_with_scripts.push((
package.clone(),
package_path.to_path_buf(),
package_path.into_owned(),
scripts_run,
));
}
Expand Down Expand Up @@ -276,11 +281,8 @@ impl<'a> LifecycleScripts<'a> {
if !self.packages_with_scripts.is_empty() {
// get custom commands for each bin available in the node_modules dir (essentially
// the scripts that are in `node_modules/.bin`)
let base = resolve_baseline_custom_commands(
snapshot,
&packages,
get_package_path,
)?;
let base =
resolve_baseline_custom_commands(snapshot, packages, get_package_path)?;
let init_cwd = self.config.initial_cwd.as_deref().unwrap();
let process_state = crate::npm::managed::npm_process_state(
snapshot.as_valid_serialized(),
Expand Down
16 changes: 6 additions & 10 deletions cli/npm/managed/resolvers/common/bin_entries.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use std::path::Path;
use std::path::PathBuf;

#[derive(Default)]
pub(in crate::npm) struct BinEntries {
pub struct BinEntries {
/// Packages that have colliding bin names
collisions: HashSet<NpmPackageId>,
seen_names: HashMap<String, NpmPackageId>,
Expand All @@ -32,16 +32,12 @@ fn default_bin_name(package: &NpmResolutionPackage) -> &str {
}

impl BinEntries {
pub(in crate::npm) fn new() -> Self {
pub fn new() -> Self {
Self::default()
}

/// Add a new bin entry (package with a bin field)
pub(in crate::npm) fn add(
&mut self,
package: NpmResolutionPackage,
package_path: PathBuf,
) {
pub fn add(&mut self, package: NpmResolutionPackage, package_path: PathBuf) {
// check for a new collision, if we haven't already
// found one
match package.bin.as_ref().unwrap() {
Expand Down Expand Up @@ -117,7 +113,7 @@ impl BinEntries {
}

/// Collect the bin entries into a vec of (name, script path)
pub(in crate::npm) fn into_bin_files(
pub fn into_bin_files(
mut self,
snapshot: &NpmResolutionSnapshot,
) -> Vec<(String, PathBuf)> {
Expand All @@ -133,7 +129,7 @@ impl BinEntries {

/// Finish setting up the bin entries, writing the necessary files
/// to disk.
pub(in crate::npm) fn finish(
pub fn finish(
mut self,
snapshot: &NpmResolutionSnapshot,
bin_node_modules_dir_path: &Path,
Expand Down Expand Up @@ -217,7 +213,7 @@ fn sort_by_depth(
});
}

pub(in crate::npm) fn set_up_bin_entry(
pub fn set_up_bin_entry(
package: &NpmResolutionPackage,
bin_name: &str,
#[allow(unused_variables)] bin_script: &str,
Expand Down
6 changes: 5 additions & 1 deletion cli/npm/managed/resolvers/global.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,11 @@ impl NpmPackageFsResolver for GlobalNpmPackageResolver {
);
for package in &package_partitions.packages {
let package_folder = self.cache.package_folder_for_nv(&package.id.nv);
lifecycle_scripts.add(package, &package_folder, &package_folder);
lifecycle_scripts.add(
package,
Cow::Borrowed(&package_folder),
&package_folder,
);
}

if lifecycle_scripts.will_run_scripts() {
Expand Down
2 changes: 1 addition & 1 deletion cli/npm/managed/resolvers/local.rs
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,7 @@ async fn sync_resolution_with_fs(
let sub_node_modules = folder_path.join("node_modules");
let package_path =
join_package_name(&sub_node_modules, &package.id.nv.name);
lifecycle_scripts.add(package, &package_path, &folder_path);
lifecycle_scripts.add(package, package_path.into(), &folder_path);
}

while let Some(result) = cache_futures.next().await {
Expand Down