Skip to content

Commit d880926

Browse files
authored
fix: ban cjs and cts file extensions (#818)
closes #713.
1 parent c7a26d2 commit d880926

File tree

7 files changed

+74
-4
lines changed

7 files changed

+74
-4
lines changed

api/src/analysis.rs

+45-1
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,7 @@ async fn analyze_package_inner(
186186
.analyzer
187187
.get_parsed_source(module.specifier())
188188
{
189+
check_for_banned_extensions(&parsed_source)?;
189190
check_for_banned_syntax(&parsed_source)?;
190191
check_for_banned_triple_slash_directives(&parsed_source)?;
191192
}
@@ -802,6 +803,21 @@ fn collect_dependencies(
802803
Ok(dependencies)
803804
}
804805

806+
fn check_for_banned_extensions(
807+
parsed_source: &ParsedSource,
808+
) -> Result<(), PublishError> {
809+
match parsed_source.media_type() {
810+
deno_ast::MediaType::Cjs | deno_ast::MediaType::Cts => {
811+
Err(PublishError::CommonJs {
812+
specifier: parsed_source.specifier().to_string(),
813+
line: 0,
814+
column: 0,
815+
})
816+
}
817+
_ => Ok(()),
818+
}
819+
}
820+
805821
fn check_for_banned_syntax(
806822
parsed_source: &ParsedSource,
807823
) -> Result<(), PublishError> {
@@ -957,8 +973,15 @@ fn check_for_banned_triple_slash_directives(
957973
#[cfg(test)]
958974
mod tests {
959975
fn parse(source: &str) -> deno_ast::ParsedSource {
960-
let specifier = deno_ast::ModuleSpecifier::parse("file:///mod.ts").unwrap();
961976
let media_type = deno_ast::MediaType::TypeScript;
977+
parse_with_media_type(source, media_type)
978+
}
979+
980+
fn parse_with_media_type(
981+
source: &str,
982+
media_type: deno_ast::MediaType,
983+
) -> deno_ast::ParsedSource {
984+
let specifier = deno_ast::ModuleSpecifier::parse("file:///mod.ts").unwrap();
962985
deno_ast::parse_module(deno_ast::ParseParams {
963986
specifier,
964987
text: source.into(),
@@ -970,6 +993,27 @@ mod tests {
970993
.unwrap()
971994
}
972995

996+
#[test]
997+
fn banned_extensions() {
998+
let x =
999+
parse_with_media_type("let x = 1;", deno_ast::MediaType::TypeScript);
1000+
assert!(super::check_for_banned_extensions(&x).is_ok());
1001+
1002+
let x = parse_with_media_type("let x = 1;", deno_ast::MediaType::Cjs);
1003+
let err = super::check_for_banned_extensions(&x).unwrap_err();
1004+
assert!(
1005+
matches!(err, super::PublishError::CommonJs { .. }),
1006+
"{err:?}",
1007+
);
1008+
1009+
let x = parse_with_media_type("let x = 1;", deno_ast::MediaType::Cts);
1010+
let err = super::check_for_banned_extensions(&x).unwrap_err();
1011+
assert!(
1012+
matches!(err, super::PublishError::CommonJs { .. }),
1013+
"{err:?}",
1014+
);
1015+
}
1016+
9731017
#[test]
9741018
fn banned_triple_slash_directives() {
9751019
let x = parse("let x = 1;");

api/src/gcp.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -573,12 +573,18 @@ impl FakeGcsTester {
573573

574574
assert!(self.proc.is_none());
575575

576-
#[cfg(target_os = "macos")]
576+
#[cfg(all(target_os = "macos", target_arch = "aarch64"))]
577577
let p = concat!(
578578
env!("CARGO_MANIFEST_DIR"),
579579
"/../tools/bin/darwin-arm64/fake-gcs-server"
580580
);
581581

582+
#[cfg(all(target_os = "macos", target_arch = "x86_64"))]
583+
let p = concat!(
584+
env!("CARGO_MANIFEST_DIR"),
585+
"/../tools/bin/darwin-amd64/fake-gcs-server"
586+
);
587+
582588
#[cfg(target_os = "linux")]
583589
let p = concat!(
584590
env!("CARGO_MANIFEST_DIR"),

api/src/publish.rs

+10
Original file line numberDiff line numberDiff line change
@@ -1309,6 +1309,16 @@ pub mod tests {
13091309
assert_eq!(task.status, PublishingTaskStatus::Success, "{task:#?}");
13101310
}
13111311

1312+
#[tokio::test]
1313+
async fn cjs_import() {
1314+
let t = TestSetup::new().await;
1315+
let bytes = create_mock_tarball("cjs_import");
1316+
let task = process_tarball_setup(&t, bytes).await;
1317+
assert_eq!(task.status, PublishingTaskStatus::Failure, "{task:#?}");
1318+
let error = task.error.unwrap();
1319+
assert_eq!(error.code, "commonJs");
1320+
}
1321+
13121322
#[tokio::test]
13131323
async fn npm_tarball() {
13141324
let t = TestSetup::new().await;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"name": "@scope/foo",
3+
"version": "1.2.3",
4+
"exports": "./mod.ts"
5+
}
+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { test } from "./other.cjs"; // bad
2+
3+
export const hello = `Hello, ${test}!`;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
exports.test = "test";

frontend/docs/troubleshooting.md

+3-2
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,9 @@ source.
9191

9292
### `commonJs`
9393

94-
The package being published contains CommonJS code like `require()`. This is
95-
disallowed because JSR is ESM only.
94+
The package being published imports code from CommonJS files with a `.cjs` or
95+
`.cts` extension or contains CommonJS code like `require()`. This is disallowed
96+
because JSR is ESM only.
9697

9798
You can fix this error by removing the CommonJS code from your source.
9899

0 commit comments

Comments
 (0)