Skip to content
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
22 changes: 21 additions & 1 deletion cli/tools/registry/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,10 @@ pub enum PublishDiagnostic {
range: SourceRange,
},
SyntaxError(ParseDiagnostic),
MissingLicense {
/// This only exists because diagnostics require a location.
expected_path: PathBuf,
},
}

impl PublishDiagnostic {
Expand Down Expand Up @@ -168,6 +172,8 @@ impl Diagnostic for PublishDiagnostic {
MissingConstraint { .. } => DiagnosticLevel::Error,
BannedTripleSlashDirectives { .. } => DiagnosticLevel::Error,
SyntaxError { .. } => DiagnosticLevel::Error,
// todo(#24676): make this an error in Deno 1.46
MissingLicense { .. } => DiagnosticLevel::Warning,
}
}

Expand All @@ -187,6 +193,7 @@ impl Diagnostic for PublishDiagnostic {
Cow::Borrowed("banned-triple-slash-directives")
}
SyntaxError { .. } => Cow::Borrowed("syntax-error"),
MissingLicense { .. } => Cow::Borrowed("missing-license"),
}
}

Expand All @@ -208,6 +215,7 @@ impl Diagnostic for PublishDiagnostic {
MissingConstraint { specifier, .. } => Cow::Owned(format!("specifier '{}' is missing a version constraint", specifier)),
BannedTripleSlashDirectives { .. } => Cow::Borrowed("triple slash directives that modify globals are not allowed"),
SyntaxError(diagnostic) => diagnostic.message(),
MissingLicense { .. } => Cow::Borrowed("missing license file"),
}
}

Expand Down Expand Up @@ -275,6 +283,9 @@ impl Diagnostic for PublishDiagnostic {
text_info: Cow::Borrowed(text_info),
},
SyntaxError(diagnostic) => diagnostic.location(),
MissingLicense { expected_path } => DiagnosticLocation::Path {
path: expected_path.clone(),
},
}
}

Expand Down Expand Up @@ -355,6 +366,7 @@ impl Diagnostic for PublishDiagnostic {
}],
}),
SyntaxError(diagnostic) => diagnostic.snippet(),
MissingLicense { .. } => None,
}
}

Expand Down Expand Up @@ -388,6 +400,9 @@ impl Diagnostic for PublishDiagnostic {
Cow::Borrowed("remove the triple slash directive"),
),
SyntaxError(diagnostic) => diagnostic.hint(),
MissingLicense { .. } => Some(
Cow::Borrowed("add a LICENSE file to the package and ensure it is not ignored from being published"),
),
}
}

Expand Down Expand Up @@ -424,7 +439,8 @@ impl Diagnostic for PublishDiagnostic {
| UnsupportedJsxTsx { .. }
| ExcludedModule { .. }
| MissingConstraint { .. }
| BannedTripleSlashDirectives { .. } => None,
| BannedTripleSlashDirectives { .. }
| MissingLicense { .. } => None,
}
}

Expand Down Expand Up @@ -474,6 +490,7 @@ impl Diagnostic for PublishDiagnostic {
Cow::Borrowed("or set their 'lib' compiler option appropriately"),
]),
SyntaxError(diagnostic) => diagnostic.info(),
MissingLicense { .. } => Cow::Borrowed(&[]),
}
}

Expand Down Expand Up @@ -507,6 +524,9 @@ impl Diagnostic for PublishDiagnostic {
"https://jsr.io/go/banned-triple-slash-directives",
)),
SyntaxError(diagnostic) => diagnostic.docs_url(),
MissingLicense { .. } => {
Some(Cow::Borrowed("https://jsr.io/go/missing-license"))
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

}
}
}
}
68 changes: 68 additions & 0 deletions cli/tools/registry/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -462,6 +462,13 @@ impl PublishPreparer {
&publish_paths,
&diagnostics_collector,
);

if !has_license_file(publish_paths.iter().map(|p| &p.specifier)) {
diagnostics_collector.push(PublishDiagnostic::MissingLicense {
expected_path: root_dir.join("LICENSE"),
});
}

tar::create_gzipped_tarball(
&publish_paths,
LazyGraphSourceParser::new(&source_cache, &graph),
Expand Down Expand Up @@ -1187,6 +1194,36 @@ async fn check_if_git_repo_dirty(cwd: &Path) -> Option<String> {
}
}

fn has_license_file<'a>(
mut specifiers: impl Iterator<Item = &'a ModuleSpecifier>,
) -> bool {
let allowed_license_files = {
let files = HashSet::from([
"license",
"license.md",
"license.txt",
"licence",
"licence.md",
"licence.txt",
]);
if cfg!(debug_assertions) {
for file in &files {
assert_eq!(*file, file.to_lowercase());
}
}
files
};
specifiers.any(|specifier| {
specifier
.path()
.rsplit_once('/')
.map(|(_, file)| {
allowed_license_files.contains(file.to_lowercase().as_str())
})
.unwrap_or(false)
})
}

#[allow(clippy::print_stderr)]
fn ring_bell() {
// ASCII code for the bell character.
Expand All @@ -1195,6 +1232,10 @@ fn ring_bell() {

#[cfg(test)]
mod tests {
use deno_ast::ModuleSpecifier;

use crate::tools::registry::has_license_file;

use super::tar::PublishableTarball;
use super::tar::PublishableTarballFile;
use super::verify_version_manifest;
Expand Down Expand Up @@ -1296,4 +1337,31 @@ mod tests {

assert!(verify_version_manifest(meta_bytes, &package).is_err());
}

#[test]
fn test_has_license_files() {
fn has_license_file_str(expected: &[&str]) -> bool {
let specifiers = expected
.iter()
.map(|s| ModuleSpecifier::parse(s).unwrap())
.collect::<Vec<_>>();
has_license_file(specifiers.iter())
}

assert!(has_license_file_str(&["file:///LICENSE"]));
assert!(has_license_file_str(&["file:///license"]));
assert!(has_license_file_str(&["file:///LICENSE.txt"]));
assert!(has_license_file_str(&["file:///LICENSE.md"]));
assert!(has_license_file_str(&["file:///LICENCE"]));
assert!(has_license_file_str(&["file:///LICENCE.txt"]));
assert!(has_license_file_str(&["file:///LICENCE.md"]));
assert!(has_license_file_str(&[
"file:///other",
"file:///test/LICENCE.md"
]),);
assert!(!has_license_file_str(&[
"file:///other",
"file:///test/tLICENSE"
]),);
}
}
2 changes: 2 additions & 0 deletions tests/integration/publish_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,7 @@ fn allow_dirty() {
}));

temp_dir.join("main.ts").write("");
temp_dir.join("LICENSE").write("");

let cmd = Command::new("git")
.arg("init")
Expand All @@ -418,6 +419,7 @@ Checking for slow types in the public API...

Uncommitted changes:

?? LICENSE
?? deno.json
?? main.ts

Expand Down
Empty file.
Empty file.
Empty file.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ Check file:///[WILDLINE]/mod.ts
Checking for slow types in the public API...
Check file:///[WILDLINE]/mod.ts
Simulating publish of @foo/[email protected] with files:
file:///[WILDLINE]/LICENSE ([WILDLINE])
file:///[WILDLINE]/deno.json (87B)
file:///[WILDLINE]/mod.ts (121B)
Warning Aborting due to --dry-run
1 change: 1 addition & 0 deletions tests/specs/publish/bare_node_builtins/no_warnings.out
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
Checking for slow types in the public API...
Simulating publish of @foo/[email protected] with files:
file:///[WILDLINE]/LICENSE ([WILDLINE])
file:///[WILDLINE]/deno.json (87B)
file:///[WILDLINE]/mod.ts (121B)
Warning Aborting due to --dry-run
Empty file.
2 changes: 1 addition & 1 deletion tests/specs/publish/byonm_dep/deno.jsonc
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"publish": {
// this was previously causing issues because it would cause
// external modules in the node_modules directory to be ignored
"include": ["mod.ts"]
"include": ["LICENSE", "mod.ts"]
},
"unstable": ["byonm", "sloppy-imports"]
}
1 change: 1 addition & 0 deletions tests/specs/publish/byonm_dep/publish.out
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ Check file:///[WILDLINE]/mod.ts
Checking for slow types in the public API...
Check file:///[WILDLINE]/mod.ts
Simulating publish of @scope/[email protected] with files:
file:///[WILDLINE]/LICENSE ([WILDLINE])
file:///[WILDLINE]/deno.jsonc ([WILDLINE])
file:///[WILDLINE]/mod.ts ([WILDLINE])
Warning Aborting due to --dry-run
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
1 change: 1 addition & 0 deletions tests/specs/publish/excluded_deno_jsonc/mod.out
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
Check file:///[WILDLINE]mod.ts
Checking for slow types in the public API...
Simulating publish of @scope/[email protected] with files:
file:///[WILDLINE]/LICENSE ([WILDLINE])
file:///[WILDLINE]/deno.jsonc (74B)
file:///[WILDLINE]/mod.ts (22B)
Warning Aborting due to --dry-run
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
4 changes: 4 additions & 0 deletions tests/specs/publish/missing_license/__test__.jsonc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"args": "publish --token 'sadfasdf'",
"output": "mod.out"
}
5 changes: 5 additions & 0 deletions tests/specs/publish/missing_license/deno.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"name": "@scope/pkg",
"version": "1.0.0",
"exports": "./mod.ts"
}
12 changes: 12 additions & 0 deletions tests/specs/publish/missing_license/mod.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
Check file:///[WILDLINE]/missing_license/mod.ts
Checking for slow types in the public API...
Check file:///[WILDLINE]/missing_license/mod.ts
warning[missing-license]: missing license file
--> [WILDLINE]LICENSE
= hint: add a LICENSE file to the package and ensure it is not ignored from being published

docs: https://jsr.io/go/missing-license

Publishing @scope/[email protected] ...
Successfully published @scope/[email protected]
Visit http://127.0.0.1:4250/@scope/[email protected] for details
3 changes: 3 additions & 0 deletions tests/specs/publish/missing_license/mod.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function add(a: number, b: number): number {
return a + b;
}
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
2 changes: 2 additions & 0 deletions tests/specs/publish/npm_workspace/publish.out
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ Check file:///[WILDLINE]/npm_workspace/add/index.ts
Check file:///[WILDLINE]/npm_workspace/subtract/index.ts
[UNORDERED_START]
Simulating publish of @scope/[email protected] with files:
file:///[WILDLINE]/npm_workspace/add/LICENSE ([WILDLINE])
file:///[WILDLINE]/npm_workspace/add/index.ts ([WILDLINE])
file:///[WILDLINE]/npm_workspace/add/jsr.json ([WILDLINE])
file:///[WILDLINE]/npm_workspace/add/package.json ([WILDLINE])
Simulating publish of @scope/[email protected] with files:
file:///[WILDLINE]/npm_workspace/subtract/LICENSE ([WILDLINE])
file:///[WILDLINE]/npm_workspace/subtract/index.ts ([WILDLINE])
file:///[WILDLINE]/npm_workspace/subtract/jsr.json ([WILDLINE])
file:///[WILDLINE]/npm_workspace/subtract/package.json ([WILDLINE])
Expand Down
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.