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
Simplify
  • Loading branch information
nathanwhit committed Sep 21, 2024
commit 09be3b2e28f12c6e845ef8bc3d6f1a2bcd12af37
21 changes: 6 additions & 15 deletions cli/npm/managed/resolvers/common/lifecycle_scripts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,22 +22,13 @@ pub trait LifecycleScriptsStrategy {
packages: &[(&NpmResolutionPackage, PathBuf)],
) -> Result<(), AnyError>;

fn has_warned(
&self,
package: &NpmResolutionPackage,
package_path: &Path,
) -> bool;
fn has_warned(&self, package: &NpmResolutionPackage) -> bool;

fn has_run(
&self,
package: &NpmResolutionPackage,
package_path: &Path,
) -> bool;
fn has_run(&self, package: &NpmResolutionPackage) -> bool;

fn did_run_scripts(
&self,
package: &NpmResolutionPackage,
package_path: &Path,
) -> Result<(), AnyError>;
}

Expand Down Expand Up @@ -117,13 +108,13 @@ impl<'a> LifecycleScripts<'a> {
std::backtrace::Backtrace::capture()
);
if self.can_run_scripts(&package.id.nv) {
if !self.strategy.has_run(package, &package_path) {
if !self.strategy.has_run(package) {
self
.packages_with_scripts
.push((package, package_path.into_owned()));
}
} else if !self.strategy.has_run(package, &package_path)
&& !self.strategy.has_warned(package, &package_path)
} else if !self.strategy.has_run(package)
&& !self.strategy.has_warned(package)
{
eprintln!("packages with scripts not run: {}", package.id.nv);
self
Expand Down Expand Up @@ -211,7 +202,7 @@ impl<'a> LifecycleScripts<'a> {
}
}
}
self.strategy.did_run_scripts(package, &package_path)?;
self.strategy.did_run_scripts(package)?;
}
}
if failed_packages.is_empty() {
Expand Down
13 changes: 2 additions & 11 deletions cli/npm/managed/resolvers/global.rs
Original file line number Diff line number Diff line change
Expand Up @@ -246,24 +246,15 @@ impl<'a> super::common::lifecycle_scripts::LifecycleScriptsStrategy
fn did_run_scripts(
&self,
_package: &NpmResolutionPackage,
_package_path: &Path,
) -> std::result::Result<(), deno_core::anyhow::Error> {
Ok(())
}

fn has_warned(
&self,
package: &NpmResolutionPackage,
_package_path: &Path,
) -> bool {
fn has_warned(&self, package: &NpmResolutionPackage) -> bool {
self.warned_scripts_file(package).exists()
}

fn has_run(
&self,
_package: &NpmResolutionPackage,
_package_path: &Path,
) -> bool {
fn has_run(&self, _package: &NpmResolutionPackage) -> bool {
false
}
}
67 changes: 22 additions & 45 deletions cli/npm/managed/resolvers/local.rs
Original file line number Diff line number Diff line change
Expand Up @@ -691,39 +691,31 @@ async fn sync_resolution_with_fs(
Ok(())
}

fn local_node_modules_package_folder_name(
fn local_package_folder_name(
local_registry_dir: &Path,
package: &NpmResolutionPackage,
) -> PathBuf {
join_package_name(
&local_registry_dir
.join(get_package_folder_id_folder_name(
&package.get_package_cache_folder_id(),
))
.join("node_modules"),
&package.id.nv.name,
)
}

fn ran_scripts_file(
local_registry_dir: &Path,
package: &NpmResolutionPackage,
) -> PathBuf {
local_node_modules_package_folder_name(local_registry_dir, package)
.join(".scripts-run")
}
fn warned_scripts_file(
local_registry_dir: &Path,
package: &NpmResolutionPackage,
) -> PathBuf {
local_node_modules_package_folder_name(local_registry_dir, package)
.join(".scripts-warned")
local_registry_dir.join(get_package_folder_id_folder_name(
&package.get_package_cache_folder_id(),
))
}

struct LocalLifecycleScripts<'a> {
deno_local_registry_dir: &'a Path,
}

impl<'a> LocalLifecycleScripts<'a> {
fn ran_scripts_file(&self, package: &NpmResolutionPackage) -> PathBuf {
local_package_folder_name(&self.deno_local_registry_dir, package)
.join(".scripts-run")
}

fn warned_scripts_file(&self, package: &NpmResolutionPackage) -> PathBuf {
local_package_folder_name(&self.deno_local_registry_dir, package)
.join(".scripts-warned")
}
}

impl<'a> super::common::lifecycle_scripts::LifecycleScriptsStrategy
for LocalLifecycleScripts<'a>
{
Expand All @@ -734,12 +726,8 @@ impl<'a> super::common::lifecycle_scripts::LifecycleScriptsStrategy
fn did_run_scripts(
&self,
package: &NpmResolutionPackage,
_package_path: &Path,
) -> std::result::Result<(), deno_core::anyhow::Error> {
std::fs::write(
ran_scripts_file(&self.deno_local_registry_dir, package),
"",
)?;
std::fs::write(self.ran_scripts_file(package), "")?;
Ok(())
}

Expand Down Expand Up @@ -774,29 +762,18 @@ impl<'a> super::common::lifecycle_scripts::LifecycleScriptsStrategy
);

for (package, _) in packages {
let _ignore_err = fs::write(
warned_scripts_file(&self.deno_local_registry_dir, package),
"",
);
let _ignore_err = fs::write(self.warned_scripts_file(package), "");
}
}
Ok(())
}

fn has_warned(
&self,
package: &NpmResolutionPackage,
_package_path: &Path,
) -> bool {
warned_scripts_file(&self.deno_local_registry_dir, package).exists()
fn has_warned(&self, package: &NpmResolutionPackage) -> bool {
self.warned_scripts_file(package).exists()
}

fn has_run(
&self,
package: &NpmResolutionPackage,
_package_path: &Path,
) -> bool {
ran_scripts_file(&self.deno_local_registry_dir, package).exists()
fn has_run(&self, package: &NpmResolutionPackage) -> bool {
self.ran_scripts_file(package).exists()
}
}

Expand Down