Skip to content

Commit

Permalink
fix(outdated): support updating dependencies in external import maps (#…
Browse files Browse the repository at this point in the history
…27339)

Fixes #27331.

The support for it was already in `outdated`, but forgot to wire up the
updating part

Needs #27337
  • Loading branch information
nathanwhit authored Dec 13, 2024
1 parent 3946956 commit 9d315f2
Show file tree
Hide file tree
Showing 9 changed files with 137 additions and 26 deletions.
61 changes: 35 additions & 26 deletions cli/tools/registry/pm/deps.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

use std::borrow::Cow;
use std::collections::HashMap;
use std::path::PathBuf;
use std::sync::atomic::AtomicBool;
use std::sync::Arc;

Expand All @@ -11,6 +12,7 @@ use deno_config::deno_json::ConfigFileRc;
use deno_config::workspace::Workspace;
use deno_config::workspace::WorkspaceDirectory;
use deno_core::anyhow::bail;
use deno_core::anyhow::Context;
use deno_core::error::AnyError;
use deno_core::futures::future::try_join;
use deno_core::futures::stream::FuturesOrdered;
Expand Down Expand Up @@ -43,10 +45,10 @@ use crate::npm::NpmFetchResolver;

use super::ConfigUpdater;

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum ImportMapKind {
Inline,
Outline,
Outline(PathBuf),
}

#[derive(Clone)]
Expand All @@ -62,9 +64,12 @@ impl DepLocation {

pub fn file_path(&self) -> Cow<std::path::Path> {
match self {
DepLocation::DenoJson(arc, _, _) => {
Cow::Owned(arc.specifier.to_file_path().unwrap())
}
DepLocation::DenoJson(arc, _, kind) => match kind {
ImportMapKind::Inline => {
Cow::Owned(arc.specifier.to_file_path().unwrap())
}
ImportMapKind::Outline(path) => Cow::Borrowed(path.as_path()),
},
DepLocation::PackageJson(arc, _) => Cow::Borrowed(arc.path.as_ref()),
}
}
Expand Down Expand Up @@ -238,22 +243,30 @@ fn to_import_map_value_from_imports(
fn deno_json_import_map(
deno_json: &ConfigFile,
) -> Result<Option<(ImportMapWithDiagnostics, ImportMapKind)>, AnyError> {
let (value, kind) =
if deno_json.json.imports.is_some() || deno_json.json.scopes.is_some() {
(
to_import_map_value_from_imports(deno_json),
ImportMapKind::Inline,
)
} else {
match deno_json.to_import_map_path()? {
Some(path) => {
let text = std::fs::read_to_string(&path)?;
let value = serde_json::from_str(&text)?;
(value, ImportMapKind::Outline)
}
None => return Ok(None),
let (value, kind) = if deno_json.json.imports.is_some()
|| deno_json.json.scopes.is_some()
{
(
to_import_map_value_from_imports(deno_json),
ImportMapKind::Inline,
)
} else {
match deno_json.to_import_map_path()? {
Some(path) => {
let err_context = || {
format!(
"loading import map at '{}' (from \"importMap\" field in '{}')",
path.display(),
deno_json.specifier
)
};
let text = std::fs::read_to_string(&path).with_context(err_context)?;
let value = serde_json::from_str(&text).with_context(err_context)?;
(value, ImportMapKind::Outline(path))
}
};
None => return Ok(None),
}
};

import_map::parse_from_value(deno_json.specifier.clone(), value)
.map_err(Into::into)
Expand Down Expand Up @@ -303,7 +316,7 @@ fn add_deps_from_deno_json(
location: DepLocation::DenoJson(
deno_json.clone(),
key_path,
import_map_kind,
import_map_kind.clone(),
),
kind,
req,
Expand Down Expand Up @@ -747,11 +760,7 @@ impl DepManager {
let dep = &mut self.deps[dep_id.0];
dep.req.version_req = version_req.clone();
match &dep.location {
DepLocation::DenoJson(arc, key_path, import_map_kind) => {
if matches!(import_map_kind, ImportMapKind::Outline) {
// not supported
continue;
}
DepLocation::DenoJson(arc, key_path, _) => {
let updater =
get_or_create_updater(&mut config_updaters, &dep.location)?;

Expand Down
24 changes: 24 additions & 0 deletions tests/specs/update/external_import_map/__test__.jsonc
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"tempDir": true,
"steps": [
{
"args": "i",
"output": "[WILDCARD]"
},
{
"args": "outdated",
"output": "outdated.out"
},
{
"args": "outdated --update --latest",
"output": "update.out"
},
{
"args": [
"eval",
"console.log(Deno.readTextFileSync('import_map.json').trim())"
],
"output": "import_map.json.out"
}
]
}
3 changes: 3 additions & 0 deletions tests/specs/update/external_import_map/deno.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"importMap": "import_map.json"
}
33 changes: 33 additions & 0 deletions tests/specs/update/external_import_map/deno.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions tests/specs/update/external_import_map/import_map.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"imports": {
"@denotest/add": "jsr:@denotest/add@^0.2.0",
"@denotest/subtract": "jsr:@denotest/subtract@^0.2.0",
"@denotest/breaking-change-between-versions": "npm:@denotest/[email protected]",
"@denotest/has-patch-versions": "npm:@denotest/has-patch-versions@^0.1.0"
}
}
8 changes: 8 additions & 0 deletions tests/specs/update/external_import_map/import_map.json.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"imports": {
"@denotest/add": "jsr:@denotest/add@^1.0.0",
"@denotest/subtract": "jsr:@denotest/subtract@^1.0.0",
"@denotest/breaking-change-between-versions": "npm:@denotest/breaking-change-between-versions@^2.0.0",
"@denotest/has-patch-versions": "npm:@denotest/has-patch-versions@^0.2.0"
}
}
1 change: 1 addition & 0 deletions tests/specs/update/external_import_map/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import { add } from "@denotest/add";
14 changes: 14 additions & 0 deletions tests/specs/update/external_import_map/outdated.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
┌────────────────────────────────────────────────┬─────────┬────────┬────────┐
│ Package │ Current │ Update │ Latest │
├────────────────────────────────────────────────┼─────────┼────────┼────────┤
│ jsr:@denotest/subtract │ 0.2.0 │ 0.2.0 │ 1.0.0 │
├────────────────────────────────────────────────┼─────────┼────────┼────────┤
│ jsr:@denotest/add │ 0.2.0 │ 0.2.1 │ 1.0.0 │
├────────────────────────────────────────────────┼─────────┼────────┼────────┤
│ npm:@denotest/has-patch-versions │ 0.1.0 │ 0.1.1 │ 0.2.0 │
├────────────────────────────────────────────────┼─────────┼────────┼────────┤
│ npm:@denotest/breaking-change-between-versions │ 1.0.0 │ 1.0.0 │ 2.0.0 │
└────────────────────────────────────────────────┴─────────┴────────┴────────┘

Run deno outdated --update --latest to update to the latest available versions,
or deno outdated --help for more information.
11 changes: 11 additions & 0 deletions tests/specs/update/external_import_map/update.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[UNORDERED_START]
Download http://127.0.0.1:4250/@denotest/subtract/1.0.0/mod.ts
Download http://127.0.0.1:4250/@denotest/add/1.0.0/mod.ts
Download http://localhost:4260/@denotest/has-patch-versions/0.2.0.tgz
Download http://localhost:4260/@denotest/breaking-change-between-versions/2.0.0.tgz
[UNORDERED_END]
Updated 4 dependencies:
- jsr:@denotest/add 0.2.0 -> 1.0.0
- jsr:@denotest/subtract 0.2.0 -> 1.0.0
- npm:@denotest/breaking-change-between-versions 1.0.0 -> 2.0.0
- npm:@denotest/has-patch-versions 0.1.0 -> 0.2.0

0 comments on commit 9d315f2

Please sign in to comment.