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

feat(unstable): ability to use a local copy of jsr packages (#25068)

This commit is contained in:
David Sherret 2024-08-20 10:11:43 -04:00 committed by GitHub
parent 19bcb40059
commit f1c58ec041
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
45 changed files with 323 additions and 26 deletions

4
Cargo.lock generated
View file

@ -1360,9 +1360,9 @@ dependencies = [
[[package]]
name = "deno_config"
version = "0.29.0"
version = "0.30.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4bc5aa53d1c3a3d0b9fd218e3f30f1080312f811b60e1866b88ec9f665447c4e"
checksum = "e287a8ab3552cf3ef8fc41b2bd7cc041c5e8e1a76de3aeb26e804b570bab37d7"
dependencies = [
"anyhow",
"deno_package_json",

View file

@ -65,7 +65,7 @@ winres.workspace = true
[dependencies]
deno_ast = { workspace = true, features = ["bundler", "cjs", "codegen", "proposal", "react", "sourcemap", "transforms", "typescript", "view", "visit"] }
deno_cache_dir = { workspace = true }
deno_config = { version = "=0.29.0", features = ["workspace", "sync"] }
deno_config = { version = "=0.30.0", features = ["workspace", "sync"] }
deno_core = { workspace = true, features = ["include_js_files_for_snapshotting"] }
deno_doc = { version = "0.146.0", features = ["html", "syntect"] }
deno_emit = "=0.44.0"

View file

@ -1085,7 +1085,7 @@ impl CliOptions {
};
Ok(
self
.start_dir
.workspace()
.create_resolver(
CreateResolverOptions {
pkg_json_dep_resolution,

View file

@ -1491,7 +1491,7 @@ impl ConfigData {
}
};
let resolver = deno_core::unsync::spawn({
let workspace = member_dir.clone();
let workspace = member_dir.workspace.clone();
let file_fetcher = file_fetcher.cloned();
async move {
workspace

View file

@ -324,7 +324,7 @@ impl ManagedCliNpmResolver {
Ok(path)
}
/// Resolves the package nv from the provided specifier.
/// Resolves the package id from the provided specifier.
pub fn resolve_pkg_id_from_specifier(
&self,
specifier: &ModuleSpecifier,

View file

@ -5,6 +5,7 @@ use dashmap::DashMap;
use dashmap::DashSet;
use deno_ast::MediaType;
use deno_config::workspace::MappedResolution;
use deno_config::workspace::MappedResolutionDiagnostic;
use deno_config::workspace::MappedResolutionError;
use deno_config::workspace::WorkspaceResolver;
use deno_core::anyhow::anyhow;
@ -21,6 +22,7 @@ use deno_graph::NpmLoadError;
use deno_graph::NpmResolvePkgReqsResult;
use deno_npm::resolution::NpmResolutionError;
use deno_package_json::PackageJsonDepValue;
use deno_runtime::colors;
use deno_runtime::deno_fs;
use deno_runtime::deno_fs::FileSystem;
use deno_runtime::deno_node::is_builtin_node_module;
@ -434,6 +436,7 @@ pub struct CliGraphResolver {
maybe_vendor_specifier: Option<ModuleSpecifier>,
found_package_json_dep_flag: AtomicFlag,
bare_node_builtins_enabled: bool,
warned_pkgs: DashSet<PackageReq>,
}
pub struct CliGraphResolverOptions<'a> {
@ -469,6 +472,7 @@ impl CliGraphResolver {
.and_then(|v| ModuleSpecifier::from_directory_path(v).ok()),
found_package_json_dep_flag: Default::default(),
bare_node_builtins_enabled: options.bare_node_builtins_enabled,
warned_pkgs: Default::default(),
}
}
@ -541,8 +545,23 @@ impl Resolver for CliGraphResolver {
});
let result = match result {
Ok(resolution) => match resolution {
MappedResolution::Normal(specifier)
| MappedResolution::ImportMap(specifier) => {
MappedResolution::Normal {
specifier,
maybe_diagnostic,
}
| MappedResolution::ImportMap {
specifier,
maybe_diagnostic,
} => {
if let Some(diagnostic) = maybe_diagnostic {
match &*diagnostic {
MappedResolutionDiagnostic::ConstraintNotMatchedLocalVersion { reference, .. } => {
if self.warned_pkgs.insert(reference.req().clone()) {
log::warn!("{} {}\n at {}", colors::yellow("Warning"), diagnostic, referrer_range);
}
}
}
}
// do sloppy imports resolution if enabled
if let Some(sloppy_imports_resolver) = &self.sloppy_imports_resolver {
Ok(

View file

@ -608,6 +608,13 @@
}
]
},
"patch": {
"type": "array",
"items": {
"type": "string"
},
"description": "UNSTABLE: List of relative paths to folders containing JSR packages to use local versions of."
},
"workspace": {
"oneOf": [
{

View file

@ -227,8 +227,8 @@ impl ModuleLoader for EmbeddedModuleLoader {
)
}
},
Ok(MappedResolution::Normal(specifier))
| Ok(MappedResolution::ImportMap(specifier)) => {
Ok(MappedResolution::Normal { specifier, .. })
| Ok(MappedResolution::ImportMap { specifier, .. }) => {
if let Ok(reference) =
NpmPackageReqReference::from_specifier(&specifier)
{
@ -622,6 +622,7 @@ pub async fn run(
.jsr_pkgs
.iter()
.map(|pkg| ResolverWorkspaceJsrPackage {
is_patch: false, // only used for enhancing the diagnostic, which isn't shown in deno compile
base: root_dir_url.join(&pkg.relative_base).unwrap(),
name: pkg.name.clone(),
version: pkg.version.clone(),

View file

@ -189,9 +189,12 @@ impl<'a> deno_graph::source::Resolver for SloppyImportCaptureResolver<'a> {
.map_err(|err| ResolveError::Other(err.into()))?;
match resolution {
deno_config::workspace::MappedResolution::Normal(specifier)
| deno_config::workspace::MappedResolution::ImportMap(specifier) => {
match self.sloppy_imports_resolver.resolve(&specifier, mode) {
deno_config::workspace::MappedResolution::Normal {
specifier, ..
}
| deno_config::workspace::MappedResolution::ImportMap {
specifier, ..
} => match self.sloppy_imports_resolver.resolve(&specifier, mode) {
Some(res) => {
self
.captures
@ -201,8 +204,7 @@ impl<'a> deno_graph::source::Resolver for SloppyImportCaptureResolver<'a> {
Ok(res.into_specifier())
}
None => Ok(specifier),
}
}
},
deno_config::workspace::MappedResolution::WorkspaceJsrPackage {
..
}

View file

@ -73,8 +73,8 @@ impl SpecifierUnfurler {
self.workspace_resolver.resolve(specifier, referrer)
{
match resolved {
MappedResolution::Normal(specifier)
| MappedResolution::ImportMap(specifier) => Some(specifier),
MappedResolution::Normal { specifier, .. }
| MappedResolution::ImportMap { specifier, .. } => Some(specifier),
MappedResolution::WorkspaceJsrPackage { pkg_req_ref, .. } => {
Some(ModuleSpecifier::parse(&pkg_req_ref.to_string()).unwrap())
}
@ -443,6 +443,7 @@ mod tests {
Arc::new(ModuleSpecifier::from_directory_path(&cwd).unwrap()),
Some(import_map),
vec![ResolverWorkspaceJsrPackage {
is_patch: false,
base: ModuleSpecifier::from_directory_path(cwd.join("jsr-package"))
.unwrap(),
name: "@denotest/example".to_string(),

View file

@ -0,0 +1,22 @@
{
"tempDir": true,
"steps": [{
"if": "unix",
"args": "compile --output my-app main/main.ts",
"output": "[WILDCARD]"
}, {
"if": "unix",
"commandName": "./my-app",
"args": [],
"output": "main.out"
}, {
"if": "windows",
"args": "compile --output my-app.exe main/main.ts",
"output": "[WILDCARD]"
}, {
"if": "windows",
"commandName": "./my-app.exe",
"args": [],
"output": "main.out"
}]
}

View file

@ -0,0 +1,4 @@
{
"name": "@denotest/add",
"exports": "./mod.ts"
}

View file

@ -0,0 +1,3 @@
export function add(a: number, b: number): number {
return (a + b) * 2; // it adds wrong
}

View file

@ -0,0 +1 @@
6

View file

@ -0,0 +1,5 @@
{
"patch": [
"../add"
]
}

View file

@ -0,0 +1,3 @@
import { add } from "jsr:@denotest/add";
console.log(add(1, 2));

View file

@ -53,6 +53,8 @@ struct MultiTestMetaData {
#[serde(default)]
pub envs: HashMap<String, String>,
#[serde(default)]
pub cwd: Option<String>,
#[serde(default)]
pub tests: BTreeMap<String, JsonMap>,
}
@ -73,6 +75,10 @@ impl MultiTestMetaData {
if multi_test_meta_data.temp_dir && !value.contains_key("tempDir") {
value.insert("tempDir".to_string(), true.into());
}
if multi_test_meta_data.cwd.is_some() && !value.contains_key("cwd") {
value
.insert("cwd".to_string(), multi_test_meta_data.cwd.clone().into());
}
if !multi_test_meta_data.envs.is_empty() {
if !value.contains_key("envs") {
value.insert("envs".to_string(), JsonMap::default().into());
@ -112,6 +118,8 @@ struct MultiStepMetaData {
#[serde(default)]
pub base: Option<String>,
#[serde(default)]
pub cwd: Option<String>,
#[serde(default)]
pub envs: HashMap<String, String>,
#[serde(default)]
pub repeat: Option<usize>,
@ -136,6 +144,7 @@ impl SingleTestMetaData {
pub fn into_multi(self) -> MultiStepMetaData {
MultiStepMetaData {
base: self.base,
cwd: None,
temp_dir: self.temp_dir,
repeat: self.repeat,
envs: Default::default(),
@ -371,7 +380,7 @@ fn run_step(
VecOrString::Vec(args) => command.args_vec(args),
VecOrString::String(text) => command.args(text),
};
let command = match &step.cwd {
let command = match step.cwd.as_ref().or(metadata.cwd.as_ref()) {
Some(cwd) => command.current_dir(cwd),
None => command,
};

View file

@ -0,0 +1,25 @@
{
"tempDir": true,
"tests": {
"matching_version": {
"cwd": "./my-pkg",
"steps": [{
"args": "test",
"output": "test.out"
}, {
"args": "lint",
"output": "Checked 2 files\n"
}]
},
"not_matching_version": {
"steps": [{
"args": "run --allow-read=. --allow-write=. modify_version.ts",
"output": "[WILDCARD]"
}, {
"cwd": "./my-pkg",
"args": "test",
"output": "not_matching_version.out"
}]
}
}
}

View file

@ -0,0 +1,5 @@
{
"name": "@denotest/add",
"version": "1.0.0",
"exports": "./mod.ts"
}

View file

@ -0,0 +1,9 @@
import { add } from "./mod.ts";
// this test should not be run or linted
Deno.test("add", () => {
let unusedVar = 5; // purposefully causing a lint error to ensure it's not linted
if (add(1, 2) !== 3) {
throw new Error("fail");
}
});

View file

@ -0,0 +1,4 @@
export function add(a: number, b: number): number {
console.log("adding", a, "and", b);
return a + b;
}

View file

@ -0,0 +1,3 @@
const data = JSON.parse(Deno.readTextFileSync("./add/deno.json"));
data.version = "2.0.0";
Deno.writeTextFileSync("./add/deno.json", JSON.stringify(data, null, 2));

View file

@ -0,0 +1,11 @@
{
"name": "@scope/my-pkg",
"version": "1.0.0",
"exports": "./mod.ts",
"imports": {
"@denotest/add": "jsr:@denotest/add@1"
},
"patch": [
"../add"
]
}

View file

@ -0,0 +1,7 @@
import { add } from "./mod.ts";
Deno.test("add", () => {
if (add(1, 2) !== 3) {
throw new Error("fail");
}
});

View file

@ -0,0 +1,2 @@
export { add } from "@denotest/add";
import "@denotest/add"; // ensure it only warns once

View file

@ -0,0 +1,11 @@
Warning Patch '@denotest/add@2.0.0' was not used because it did not match '@denotest/add@1'
at file:///[WILDLINE]/my-pkg/mod.ts:1:21
Download http://127.0.0.1:4250/@denotest/add/meta.json
Download http://127.0.0.1:4250/@denotest/add/1.0.0_meta.json
Download http://127.0.0.1:4250/@denotest/add/1.0.0/mod.ts
Check file:///[WILDLINE]/mod.test.ts
running 1 test from ./mod.test.ts
add ... ok ([WILDLINE])
ok | 1 passed | 0 failed ([WILDLINE])

View file

@ -0,0 +1,10 @@
Check file:///[WILDLINE]/mod.test.ts
running 1 test from ./mod.test.ts
add ...
------- output -------
adding 1 and 2
----- output end -----
add ... ok ([WILDLINE])
ok | 1 passed | 0 failed ([WILDLINE])

View file

@ -0,0 +1,4 @@
{
"args": "test",
"output": "test.out"
}

View file

@ -0,0 +1,5 @@
{
"name": "@denotest/add",
"version": "2.0.0",
"exports": "./mod.ts"
}

View file

@ -0,0 +1,4 @@
export function add(a: number, b: number): number {
console.log("local");
return a + b;
}

View file

@ -0,0 +1,3 @@
{
"workspace": ["./add", "./subtract"]
}

View file

@ -0,0 +1,23 @@
{
"version": "3",
"packages": {
"specifiers": {
"jsr:@denotest/add@1": "jsr:@denotest/add@1.0.0"
},
"jsr": {
"@denotest/add@1.0.0": {
"integrity": "3b2e675c1ad7fba2a45bc251992e01aff08a3c974ac09079b11e6a5b95d4bfcb"
}
}
},
"remote": {},
"workspace": {
"members": {
"subtract": {
"dependencies": [
"jsr:@denotest/add@1"
]
}
}
}
}

View file

@ -0,0 +1,8 @@
{
"name": "@denotest/subtract",
"version": "1.0.0",
"exports": "./mod.ts",
"imports": {
"@denotest/add": "jsr:@denotest/add@1"
}
}

View file

@ -0,0 +1,7 @@
import { subtract } from "./mod.ts";
Deno.test("subtract", () => {
if (subtract(3, 2) !== 1) {
throw new Error("fail");
}
});

View file

@ -0,0 +1,5 @@
import { add } from "@denotest/add";
export function subtract(a: number, b: number): number {
return add(a, -b);
}

View file

@ -0,0 +1,11 @@
Warning Workspace member '@denotest/add@2.0.0' was not used because it did not match '@denotest/add@1'
at file:///[WILDLINE]/mod.ts:1:21
Download http://127.0.0.1:4250/@denotest/add/meta.json
Download http://127.0.0.1:4250/@denotest/add/1.0.0_meta.json
Download http://127.0.0.1:4250/@denotest/add/1.0.0/mod.ts
Check file:///[WILDLINE]/mod.test.ts
running 1 test from ./subtract/mod.test.ts
subtract ... ok ([WILDLINE])
ok | 1 passed | 0 failed ([WILDLINE])

View file

@ -0,0 +1,13 @@
{
"cwd": "./workspace",
"tests": {
"lint": {
"args": "lint",
"output": "Checked 2 files\n"
},
"test": {
"args": "test",
"output": "test.out"
}
}
}

View file

@ -0,0 +1,5 @@
{
"name": "@denotest/add",
"version": "1.0.0",
"exports": "./mod.ts"
}

View file

@ -0,0 +1,9 @@
import { add } from "./mod.ts";
// this test should not be run or linted
Deno.test("add", () => {
let unusedVar = 5; // purposefully causing a lint error to ensure it's not linted
if (add(1, 2) !== 3) {
throw new Error("fail");
}
});

View file

@ -0,0 +1,4 @@
export function add(a: number, b: number): number {
console.log("adding", a, "and", b);
return a + b;
}

View file

@ -0,0 +1,10 @@
Check file:///[WILDLINE]/my-pkg/mod.test.ts
running 1 test from ./my-pkg/mod.test.ts
add ...
------- output -------
adding 1 and 2
----- output end -----
add ... ok ([WILDLINE])
ok | 1 passed | 0 failed ([WILDLINE])

View file

@ -0,0 +1,6 @@
{
"workspace": ["./my-pkg"],
"patch": [
"../add"
]
}

View file

@ -0,0 +1,8 @@
{
"name": "@scope/my-pkg",
"version": "1.0.0",
"exports": "./mod.ts",
"imports": {
"@denotest/add": "jsr:@denotest/add@1"
}
}

View file

@ -0,0 +1,7 @@
import { add } from "./mod.ts";
Deno.test("add", () => {
if (add(1, 2) !== 3) {
throw new Error("fail");
}
});

View file

@ -0,0 +1 @@
export { add } from "@denotest/add";