summaryrefslogtreecommitdiff
path: root/cli/lsp/diagnostics.rs
blob: caabd3f04e2965fba83901248bee803d676c0966 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.

use super::analysis;
use super::client::Client;
use super::config::Config;
use super::documents;
use super::documents::Document;
use super::documents::Documents;
use super::documents::DocumentsFilter;
use super::language_server;
use super::language_server::StateSnapshot;
use super::performance::Performance;
use super::tsc;
use super::tsc::TsServer;
use super::urls::uri_parse_unencoded;
use super::urls::url_to_uri;
use super::urls::LspUrlMap;

use crate::graph_util;
use crate::graph_util::enhanced_resolution_error_message;
use crate::lsp::lsp_custom::DiagnosticBatchNotificationParams;
use crate::resolver::CliSloppyImportsResolver;
use crate::resolver::SloppyImportsCachedFs;
use crate::tools::lint::CliLinter;
use crate::tools::lint::CliLinterOptions;
use crate::tools::lint::LintRuleProvider;
use crate::util::path::to_percent_decoded_str;

use deno_ast::MediaType;
use deno_config::deno_json::LintConfig;
use deno_core::anyhow::anyhow;
use deno_core::error::AnyError;
use deno_core::parking_lot::RwLock;
use deno_core::resolve_url;
use deno_core::serde::Deserialize;
use deno_core::serde_json;
use deno_core::serde_json::json;
use deno_core::unsync::spawn;
use deno_core::unsync::spawn_blocking;
use deno_core::unsync::JoinHandle;
use deno_core::url::Url;
use deno_core::ModuleSpecifier;
use deno_graph::source::ResolveError;
use deno_graph::Resolution;
use deno_graph::ResolutionError;
use deno_graph::SpecifierError;
use deno_resolver::sloppy_imports::SloppyImportsResolution;
use deno_resolver::sloppy_imports::SloppyImportsResolutionMode;
use deno_runtime::deno_fs;
use deno_runtime::deno_node;
use deno_runtime::tokio_util::create_basic_runtime;
use deno_semver::jsr::JsrPackageReqReference;
use deno_semver::npm::NpmPackageReqReference;
use deno_semver::package::PackageReq;
use import_map::ImportMap;
use import_map::ImportMapError;
use log::error;
use std::collections::HashMap;
use std::collections::HashSet;
use std::path::PathBuf;
use std::sync::atomic::AtomicUsize;
use std::sync::Arc;
use std::thread;
use tokio::sync::mpsc;
use tokio::sync::Mutex;
use tokio::time::Duration;
use tokio_util::sync::CancellationToken;
use tower_lsp::lsp_types as lsp;

#[derive(Debug)]
pub struct DiagnosticServerUpdateMessage {
  pub snapshot: Arc<StateSnapshot>,
  pub url_map: LspUrlMap,
}

#[derive(Debug)]
struct DiagnosticRecord {
  pub specifier: ModuleSpecifier,
  pub versioned: VersionedDiagnostics,
}

#[derive(Clone, Default, Debug)]
struct VersionedDiagnostics {
  pub version: Option<i32>,
  pub diagnostics: Vec<lsp::Diagnostic>,
}

type DiagnosticVec = Vec<DiagnosticRecord>;

#[derive(Debug, Hash, PartialEq, Eq, Copy, Clone)]
pub enum DiagnosticSource {
  Deno,
  Lint,
  Ts,
}

impl DiagnosticSource {
  pub fn as_lsp_source(&self) -> &'static str {
    match self {
      Self::Deno => "deno",
      Self::Lint => "deno-lint",
      Self::Ts => "deno-ts",
    }
  }
}

type DiagnosticsBySource = HashMap<DiagnosticSource, VersionedDiagnostics>;

#[derive(Debug)]
struct DiagnosticsPublisher {
  client: Client,
  state: Arc<DiagnosticsState>,
  diagnostics_by_specifier:
    Mutex<HashMap<ModuleSpecifier, DiagnosticsBySource>>,
}

impl DiagnosticsPublisher {
  pub fn new(client: Client, state: Arc<DiagnosticsState>) -> Self {
    Self {
      client,
      state,
      diagnostics_by_specifier: Default::default(),
    }
  }

  pub async fn publish(
    &self,
    source: DiagnosticSource,
    diagnostics: DiagnosticVec,
    url_map: &LspUrlMap,
    documents: &Documents,
    token: &CancellationToken,
  ) -> usize {
    let mut diagnostics_by_specifier =
      self.diagnostics_by_specifier.lock().await;
    let mut seen_specifiers = HashSet::with_capacity(diagnostics.len());
    let mut messages_sent = 0;

    for record in diagnostics {
      if token.is_cancelled() {
        return messages_sent;
      }

      seen_specifiers.insert(record.specifier.clone());

      let diagnostics_by_source = diagnostics_by_specifier
        .entry(record.specifier.clone())
        .or_default();
      let version = record.versioned.version;
      let source_diagnostics = diagnostics_by_source.entry(source).or_default();
      *source_diagnostics = record.versioned;

      // DO NOT filter these by version. We want to display even out
      // of date diagnostics in order to prevent flickering. The user's
      // lsp client will eventually catch up.
      let all_specifier_diagnostics = diagnostics_by_source
        .values()
        .flat_map(|d| &d.diagnostics)
        .cloned()
        .collect::<Vec<_>>();

      self
        .state
        .update(&record.specifier, version, &all_specifier_diagnostics);
      let file_referrer = documents.get_file_referrer(&record.specifier);
      let Ok(uri) =
        url_map.specifier_to_uri(&record.specifier, file_referrer.as_deref())
      else {
        continue;
      };
      self
        .client
        .publish_diagnostics(uri, all_specifier_diagnostics, version)
        .await;
      messages_sent += 1;
    }

    // now check all the specifiers to clean up any ones with old diagnostics
    let mut specifiers_to_remove = Vec::new();
    for (specifier, diagnostics_by_source) in
      diagnostics_by_specifier.iter_mut()
    {
      if seen_specifiers.contains(specifier) {
        continue;
      }
      if token.is_cancelled() {
        break;
      }
      let maybe_removed_value = diagnostics_by_source.remove(&source);
      if diagnostics_by_source.is_empty() {
        specifiers_to_remove.push(specifier.clone());
        if let Some(removed_value) = maybe_removed_value {
          // clear out any diagnostics for this specifier
          self.state.update(specifier, removed_value.version, &[]);
          let file_referrer = documents.get_file_referrer(specifier);
          let Ok(uri) =
            url_map.specifier_to_uri(specifier, file_referrer.as_deref())
          else {
            continue;
          };
          self
            .client
            .publish_diagnostics(uri, Vec::new(), removed_value.version)
            .await;
          messages_sent += 1;
        }
      }
    }

    // clean up specifiers with no diagnostics
    for specifier in specifiers_to_remove {
      diagnostics_by_specifier.remove(&specifier);
    }

    messages_sent
  }

  pub async fn clear(&self) {
    let mut all_diagnostics = self.diagnostics_by_specifier.lock().await;
    all_diagnostics.clear();
  }
}

type DiagnosticMap = HashMap<ModuleSpecifier, VersionedDiagnostics>;

#[derive(Clone, Default, Debug)]
struct TsDiagnosticsStore(Arc<deno_core::parking_lot::Mutex<DiagnosticMap>>);

impl TsDiagnosticsStore {
  pub fn get(
    &self,
    specifier: &ModuleSpecifier,
    document_version: Option<i32>,
  ) -> Vec<lsp::Diagnostic> {
    let ts_diagnostics = self.0.lock();
    if let Some(versioned) = ts_diagnostics.get(specifier) {
      // only get the diagnostics if they're up to date
      if document_version == versioned.version {
        return versioned.diagnostics.clone();
      }
    }
    Vec::new()
  }

  pub fn invalidate(&self, specifiers: &[ModuleSpecifier]) {
    let mut ts_diagnostics = self.0.lock();
    for specifier in specifiers {
      ts_diagnostics.remove(specifier);
    }
  }

  pub fn invalidate_all(&self) {
    self.0.lock().clear();
  }

  fn update(&self, diagnostics: &DiagnosticVec) {
    let mut stored_ts_diagnostics = self.0.lock();
    *stored_ts_diagnostics = diagnostics
      .iter()
      .map(|record| (record.specifier.clone(), record.versioned.clone()))
      .collect();
  }
}

pub fn should_send_diagnostic_batch_index_notifications() -> bool {
  crate::args::has_flag_env_var(
    "DENO_DONT_USE_INTERNAL_LSP_DIAGNOSTIC_SYNC_FLAG",
  )
}

#[derive(Clone, Debug)]
struct DiagnosticBatchCounter(Option<Arc<AtomicUsize>>);

impl Default for DiagnosticBatchCounter {
  fn default() -> Self {
    if should_send_diagnostic_batch_index_notifications() {
      Self(Some(Default::default()))
    } else {
      Self(None)
    }
  }
}

impl DiagnosticBatchCounter {
  pub fn inc(&self) -> Option<usize> {
    self
      .0
      .as_ref()
      .map(|value| value.fetch_add(1, std::sync::atomic::Ordering::SeqCst) + 1)
  }

  pub fn get(&self) -> Option<usize> {
    self
      .0
      .as_ref()
      .map(|value| value.load(std::sync::atomic::Ordering::SeqCst))
  }
}

#[derive(Debug)]
enum ChannelMessage {
  Update(ChannelUpdateMessage),
  Clear,
}

#[derive(Debug)]
struct ChannelUpdateMessage {
  message: DiagnosticServerUpdateMessage,
  batch_index: Option<usize>,
}

#[derive(Debug)]
struct SpecifierState {
  version: Option<i32>,
  no_cache_diagnostics: Vec<lsp::Diagnostic>,
}

#[derive(Debug, Default)]
pub struct DiagnosticsState {
  specifiers: RwLock<HashMap<ModuleSpecifier, SpecifierState>>,
}

impl DiagnosticsState {
  fn update(
    &self,
    specifier: &ModuleSpecifier,
    version: Option<i32>,
    diagnostics: &[lsp::Diagnostic],
  ) {
    let mut specifiers = self.specifiers.write();
    let current_version = specifiers.get(specifier).and_then(|s| s.version);
    match (version, current_version) {
      (Some(arg), Some(existing)) if arg < existing => return,
      _ => {}
    }
    let mut no_cache_diagnostics = vec![];
    for diagnostic in diagnostics {
      if diagnostic.code
        == Some(lsp::NumberOrString::String("no-cache".to_string()))
        || diagnostic.code
          == Some(lsp::NumberOrString::String("not-installed-jsr".to_string()))
        || diagnostic.code
          == Some(lsp::NumberOrString::String("not-installed-npm".to_string()))
      {
        no_cache_diagnostics.push(diagnostic.clone());
      }
    }
    specifiers.insert(
      specifier.clone(),
      SpecifierState {
        version,
        no_cache_diagnostics,
      },
    );
  }

  pub fn clear(&self, specifier: &ModuleSpecifier) {
    self.specifiers.write().remove(specifier);
  }

  pub fn has_no_cache_diagnostics(&self, specifier: &ModuleSpecifier) -> bool {
    self
      .specifiers
      .read()
      .get(specifier)
      .map(|s| !s.no_cache_diagnostics.is_empty())
      .unwrap_or(false)
  }

  pub fn no_cache_diagnostics(
    &self,
    specifier: &ModuleSpecifier,
  ) -> Vec<lsp::Diagnostic> {
    self
      .specifiers
      .read()
      .get(specifier)
      .map(|s| s.no_cache_diagnostics.clone())
      .unwrap_or_default()
  }
}

#[derive(Debug)]
pub struct DiagnosticsServer {
  channel: Option<mpsc::UnboundedSender<ChannelMessage>>,
  ts_diagnostics: TsDiagnosticsStore,
  client: Client,
  performance: Arc<Performance>,
  ts_server: Arc<TsServer>,
  batch_counter: DiagnosticBatchCounter,
  state: Arc<DiagnosticsState>,
}

impl DiagnosticsServer {
  pub fn new(
    client: Client,
    performance: Arc<Performance>,
    ts_server: Arc<TsServer>,
    state: Arc<DiagnosticsState>,
  ) -> Self {
    DiagnosticsServer {
      channel: Default::default(),
      ts_diagnostics: Default::default(),
      client,
      performance,
      ts_server,
      batch_counter: Default::default(),
      state,
    }
  }

  pub fn get_ts_diagnostics(
    &self,
    specifier: &ModuleSpecifier,
    document_version: Option<i32>,
  ) -> Vec<lsp::Diagnostic> {
    self.ts_diagnostics.get(specifier, document_version)
  }

  pub fn invalidate(&self, specifiers: &[ModuleSpecifier]) {
    self.ts_diagnostics.invalidate(specifiers);
  }

  pub fn invalidate_all(&self) {
    self.ts_diagnostics.invalidate_all();
    if let Some(tx) = &self.channel {
      let _ = tx.send(ChannelMessage::Clear);
    }
  }

  #[allow(unused_must_use)]
  pub fn start(&mut self) {
    let (tx, mut rx) = mpsc::unbounded_channel::<ChannelMessage>();
    self.channel = Some(tx);
    let client = self.client.clone();
    let state = self.state.clone();
    let performance = self.performance.clone();
    let ts_diagnostics_store = self.ts_diagnostics.clone();
    let ts_server = self.ts_server.clone();

    let _join_handle = thread::spawn(move || {
      let runtime = create_basic_runtime();

      runtime.block_on(async {
        let mut token = CancellationToken::new();
        let mut ts_handle: Option<JoinHandle<()>> = None;
        let mut lint_handle: Option<JoinHandle<()>> = None;
        let mut deps_handle: Option<JoinHandle<()>> = None;
        let diagnostics_publisher =
          Arc::new(DiagnosticsPublisher::new(client.clone(), state.clone()));

        loop {
          match rx.recv().await {
            // channel has closed
            None => break,
            Some(message) => {
              let message = match message {
                ChannelMessage::Update(message) => message,
                ChannelMessage::Clear => {
                  token.cancel();
                  token = CancellationToken::new();
                  diagnostics_publisher.clear().await;
                  continue;
                }
              };
              let ChannelUpdateMessage {
                message: DiagnosticServerUpdateMessage { snapshot, url_map },
                batch_index,
              } = message;
              let url_map = Arc::new(url_map);

              // cancel the previous run
              token.cancel();
              token = CancellationToken::new();

              let previous_ts_handle = ts_handle.take();
              ts_handle = Some(spawn({
                let performance = performance.clone();
                let diagnostics_publisher = diagnostics_publisher.clone();
                let ts_server = ts_server.clone();
                let token = token.clone();
                let ts_diagnostics_store = ts_diagnostics_store.clone();
                let snapshot = snapshot.clone();
                let config = snapshot.config.clone();
                let url_map = url_map.clone();
                async move {
                  if let Some(previous_handle) = previous_ts_handle {
                    // Wait on the previous run to complete in order to prevent
                    // multiple threads queueing up a lot of tsc requests.
                    // Do not race this with cancellation because we want a
                    // chain of events to wait for all the previous diagnostics to complete
                    previous_handle.await;
                  }

                  // Debounce timer delay. 150ms between keystrokes is about 45 WPM, so we
                  // want something that is longer than that, but not too long to
                  // introduce detectable UI delay; 200ms is a decent compromise.
                  const DELAY: Duration = Duration::from_millis(200);
                  tokio::select! {
                    _ = token.cancelled() => { return; }
                    _ = tokio::time::sleep(DELAY) => {}
                  };

                  let mark = performance.mark("lsp.update_diagnostics_ts");
                  let diagnostics = generate_ts_diagnostics(
                    snapshot.clone(),
                    &config,
                    &ts_server,
                    token.clone(),
                  )
                  .await
                  .map_err(|err| {
                    if !token.is_cancelled() {
                      error!(
                        "Error generating TypeScript diagnostics: {}",
                        err
                      );
                      token.cancel();
                    }
                  })
                  .unwrap_or_default();

                  let mut messages_len = 0;
                  if !token.is_cancelled() {
                    ts_diagnostics_store.update(&diagnostics);
                    messages_len = diagnostics_publisher
                      .publish(
                        DiagnosticSource::Ts,
                        diagnostics,
                        &url_map,
                        snapshot.documents.as_ref(),
                        &token,
                      )
                      .await;

                    if !token.is_cancelled() {
                      performance.measure(mark);
                    }
                  }

                  if let Some(batch_index) = batch_index {
                    diagnostics_publisher
                      .client
                      .send_diagnostic_batch_notification(
                        DiagnosticBatchNotificationParams {
                          batch_index,
                          messages_len,
                        },
                      );
                  }
                }
              }));

              let previous_deps_handle = deps_handle.take();
              deps_handle = Some(spawn({
                let performance = performance.clone();
                let diagnostics_publisher = diagnostics_publisher.clone();
                let token = token.clone();
                let snapshot = snapshot.clone();
                let config = snapshot.config.clone();
                let url_map = url_map.clone();
                async move {
                  if let Some(previous_handle) = previous_deps_handle {
                    previous_handle.await;
                  }
                  let mark = performance.mark("lsp.update_diagnostics_deps");
                  let diagnostics = spawn_blocking({
                    let token = token.clone();
                    let snapshot = snapshot.clone();
                    move || generate_deno_diagnostics(&snapshot, &config, token)
                  })
                  .await
                  .unwrap();

                  let mut messages_len = 0;
                  if !token.is_cancelled() {
                    messages_len = diagnostics_publisher
                      .publish(
                        DiagnosticSource::Deno,
                        diagnostics,
                        &url_map,
                        snapshot.documents.as_ref(),
                        &token,
                      )
                      .await;

                    if !token.is_cancelled() {
                      performance.measure(mark);
                    }
                  }

                  if let Some(batch_index) = batch_index {
                    diagnostics_publisher
                      .client
                      .send_diagnostic_batch_notification(
                        DiagnosticBatchNotificationParams {
                          batch_index,
                          messages_len,
                        },
                      );
                  }
                }
              }));

              let previous_lint_handle = lint_handle.take();
              lint_handle = Some(spawn({
                let performance = performance.clone();
                let diagnostics_publisher = diagnostics_publisher.clone();
                let token = token.clone();
                let snapshot = snapshot.clone();
                let config = snapshot.config.clone();
                let url_map = url_map.clone();
                async move {
                  if let Some(previous_handle) = previous_lint_handle {
                    previous_handle.await;
                  }
                  let mark = performance.mark("lsp.update_diagnostics_lint");
                  let diagnostics = spawn_blocking({
                    let token = token.clone();
                    let snapshot = snapshot.clone();
                    move || generate_lint_diagnostics(&snapshot, &config, token)
                  })
                  .await
                  .unwrap();

                  let mut messages_len = 0;
                  if !token.is_cancelled() {
                    messages_len = diagnostics_publisher
                      .publish(
                        DiagnosticSource::Lint,
                        diagnostics,
                        &url_map,
                        snapshot.documents.as_ref(),
                        &token,
                      )
                      .await;

                    if !token.is_cancelled() {
                      performance.measure(mark);
                    }
                  }

                  if let Some(batch_index) = batch_index {
                    diagnostics_publisher
                      .client
                      .send_diagnostic_batch_notification(
                        DiagnosticBatchNotificationParams {
                          batch_index,
                          messages_len,
                        },
                      );
                  }
                }
              }));
            }
          }
        }
      })
    });
  }

  pub fn latest_batch_index(&self) -> Option<usize> {
    self.batch_counter.get()
  }

  pub fn update(
    &self,
    message: DiagnosticServerUpdateMessage,
  ) -> Result<(), AnyError> {
    // todo(dsherret): instead of queuing up messages, it would be better to
    // instead only store the latest message (ex. maybe using a
    // tokio::sync::watch::channel)
    if let Some(tx) = &self.channel {
      tx.send(ChannelMessage::Update(ChannelUpdateMessage {
        message,
        batch_index: self.batch_counter.inc(),
      }))
      .map_err(|err| err.into())
    } else {
      Err(anyhow!("diagnostics server not started"))
    }
  }
}

impl<'a> From<&'a crate::tsc::DiagnosticCategory> for lsp::DiagnosticSeverity {
  fn from(category: &'a crate::tsc::DiagnosticCategory) -> Self {
    match category {
      crate::tsc::DiagnosticCategory::Error => lsp::DiagnosticSeverity::ERROR,
      crate::tsc::DiagnosticCategory::Warning => {
        lsp::DiagnosticSeverity::WARNING
      }
      crate::tsc::DiagnosticCategory::Suggestion => {
        lsp::DiagnosticSeverity::HINT
      }
      crate::tsc::DiagnosticCategory::Message => {
        lsp::DiagnosticSeverity::INFORMATION
      }
    }
  }
}

impl<'a> From<&'a crate::tsc::Position> for lsp::Position {
  fn from(pos: &'a crate::tsc::Position) -> Self {
    Self {
      line: pos.line as u32,
      character: pos.character as u32,
    }
  }
}

fn get_diagnostic_message(diagnostic: &crate::tsc::Diagnostic) -> String {
  if let Some(message) = diagnostic.message_text.clone() {
    message
  } else if let Some(message_chain) = diagnostic.message_chain.clone() {
    message_chain.format_message(0)
  } else {
    "[missing message]".to_string()
  }
}

fn to_lsp_range(
  start: &crate::tsc::Position,
  end: &crate::tsc::Position,
) -> lsp::Range {
  lsp::Range {
    start: start.into(),
    end: end.into(),
  }
}

fn to_lsp_related_information(
  related_information: &Option<Vec<crate::tsc::Diagnostic>>,
) -> Option<Vec<lsp::DiagnosticRelatedInformation>> {
  related_information.as_ref().map(|related| {
    related
      .iter()
      .filter_map(|ri| {
        if let (Some(file_name), Some(start), Some(end)) =
          (&ri.file_name, &ri.start, &ri.end)
        {
          let uri = uri_parse_unencoded(file_name).unwrap();
          Some(lsp::DiagnosticRelatedInformation {
            location: lsp::Location {
              uri,
              range: to_lsp_range(start, end),
            },
            message: get_diagnostic_message(ri),
          })
        } else {
          None
        }
      })
      .collect()
  })
}

fn ts_json_to_diagnostics(
  diagnostics: Vec<crate::tsc::Diagnostic>,
) -> Vec<lsp::Diagnostic> {
  diagnostics
    .iter()
    .filter_map(|d| {
      if let (Some(start), Some(end)) = (&d.start, &d.end) {
        Some(lsp::Diagnostic {
          range: to_lsp_range(start, end),
          severity: Some((&d.category).into()),
          code: Some(lsp::NumberOrString::Number(d.code as i32)),
          code_description: None,
          source: Some(DiagnosticSource::Ts.as_lsp_source().to_string()),
          message: get_diagnostic_message(d),
          related_information: to_lsp_related_information(
            &d.related_information,
          ),
          tags: match d.code {
            // These are codes that indicate the variable is unused.
            2695 | 6133 | 6138 | 6192 | 6196 | 6198 | 6199 | 6205 | 7027
            | 7028 => Some(vec![lsp::DiagnosticTag::UNNECESSARY]),
            // These are codes that indicated the variable is deprecated.
            2789 | 6385 | 6387 => Some(vec![lsp::DiagnosticTag::DEPRECATED]),
            _ => None,
          },
          data: None,
        })
      } else {
        None
      }
    })
    .collect()
}

fn generate_lint_diagnostics(
  snapshot: &language_server::StateSnapshot,
  config: &Config,
  token: CancellationToken,
) -> DiagnosticVec {
  let documents = snapshot
    .documents
    .documents(DocumentsFilter::OpenDiagnosable);
  let config_data_by_scope = config.tree.data_by_scope();
  let mut diagnostics_vec = Vec::new();
  for document in documents {
    let specifier = document.specifier();
    if specifier.scheme() != "file" {
      continue;
    }
    if !config.specifier_enabled(specifier) {
      continue;
    }
    let settings = config.workspace_settings_for_specifier(specifier);
    if !settings.lint {
      continue;
    }
    // exit early if cancelled
    if token.is_cancelled() {
      break;
    }
    // ignore any npm package files
    if snapshot.resolver.in_node_modules(specifier) {
      continue;
    }
    let version = document.maybe_lsp_version();
    let (lint_config, linter) = config
      .tree
      .scope_for_specifier(specifier)
      .and_then(|s| config_data_by_scope.get(s))
      .map(|d| (d.lint_config.clone(), d.linter.clone()))
      .unwrap_or_else(|| {
        (
          Arc::new(LintConfig::new_with_base(PathBuf::from("/"))),
          Arc::new(CliLinter::new(CliLinterOptions {
            configured_rules: {
              let lint_rule_provider = LintRuleProvider::new(None, None);
              lint_rule_provider.resolve_lint_rules(Default::default(), None)
            },
            fix: false,
            deno_lint_config: deno_lint::linter::LintConfig {
              default_jsx_factory: None,
              default_jsx_fragment_factory: None,
            },
          })),
        )
      });
    diagnostics_vec.push(DiagnosticRecord {
      specifier: specifier.clone(),
      versioned: VersionedDiagnostics {
        version,
        diagnostics: generate_document_lint_diagnostics(
          &document,
          &lint_config,
          &linter,
        ),
      },
    });
  }
  diagnostics_vec
}

fn generate_document_lint_diagnostics(
  document: &Document,
  lint_config: &LintConfig,
  linter: &CliLinter,
) -> Vec<lsp::Diagnostic> {
  if !lint_config.files.matches_specifier(document.specifier()) {
    return Vec::new();
  }
  match document.maybe_parsed_source() {
    Some(Ok(parsed_source)) => {
      if let Ok(references) =
        analysis::get_lint_references(parsed_source, linter)
      {
        references
          .into_iter()
          .map(|r| r.to_diagnostic())
          .collect::<Vec<_>>()
      } else {
        Vec::new()
      }
    }
    Some(Err(_)) => Vec::new(),
    None => {
      error!("Missing file contents for: {}", document.specifier());
      Vec::new()
    }
  }
}

async fn generate_ts_diagnostics(
  snapshot: Arc<language_server::StateSnapshot>,
  config: &Config,
  ts_server: &tsc::TsServer,
  token: CancellationToken,
) -> Result<DiagnosticVec, AnyError> {
  let mut diagnostics_vec = Vec::new();
  let specifiers = snapshot
    .documents
    .documents(DocumentsFilter::OpenDiagnosable)
    .into_iter()
    .map(|d| d.specifier().clone());
  let (enabled_specifiers, disabled_specifiers) = specifiers
    .into_iter()
    .partition::<Vec<_>, _>(|s| config.specifier_enabled(s));
  let ts_diagnostics_map = if !enabled_specifiers.is_empty() {
    ts_server
      .get_diagnostics(snapshot.clone(), enabled_specifiers, token)
      .await?
  } else {
    Default::default()
  };
  for (specifier_str, ts_json_diagnostics) in ts_diagnostics_map {
    let specifier = resolve_url(&specifier_str)?;
    let version = snapshot
      .documents
      .get(&specifier)
      .and_then(|d| d.maybe_lsp_version());
    // check if the specifier is enabled again just in case TS returns us
    // diagnostics for a disabled specifier
    let ts_diagnostics = if config.specifier_enabled(&specifier) {
      ts_json_to_diagnostics(ts_json_diagnostics)
    } else {
      Vec::new()
    };
    diagnostics_vec.push(DiagnosticRecord {
      specifier,
      versioned: VersionedDiagnostics {
        version,
        diagnostics: ts_diagnostics,
      },
    });
  }
  // add an empty diagnostic publish for disabled specifiers in order
  // to clear those diagnostics if they exist
  for specifier in disabled_specifiers {
    let version = snapshot
      .documents
      .get(&specifier)
      .and_then(|d| d.maybe_lsp_version());
    diagnostics_vec.push(DiagnosticRecord {
      specifier,
      versioned: VersionedDiagnostics {
        version,
        diagnostics: Vec::new(),
      },
    });
  }
  Ok(diagnostics_vec)
}

#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct DiagnosticDataSpecifier {
  pub specifier: ModuleSpecifier,
}

#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
struct DiagnosticDataStrSpecifier {
  pub specifier: String,
}

#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
struct DiagnosticDataRedirect {
  pub redirect: ModuleSpecifier,
}

#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
struct DiagnosticDataNoLocal {
  pub to: ModuleSpecifier,
  pub message: String,
}

#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
struct DiagnosticDataImportMapRemap {
  pub from: String,
  pub to: String,
}

/// An enum which represents diagnostic errors which originate from Deno itself.
pub enum DenoDiagnostic {
  /// A `x-deno-warning` is associated with the specifier and should be displayed
  /// as a warning to the user.
  DenoWarn(String),
  /// An informational diagnostic that indicates an existing specifier can be
  /// remapped to an import map import specifier.
  ImportMapRemap { from: String, to: String },
  /// The import assertion type is incorrect.
  InvalidAttributeType(String),
  /// A module requires an attribute type to be a valid import.
  NoAttributeType,
  /// A remote module was not found in the cache.
  NoCache(ModuleSpecifier),
  /// A remote jsr package reference was not found in the cache.
  NotInstalledJsr(PackageReq, ModuleSpecifier),
  /// A remote npm package reference was not found in the cache.
  NotInstalledNpm(PackageReq, ModuleSpecifier),
  /// A local module was not found on the local file system.
  NoLocal(ModuleSpecifier),
  /// The specifier resolved to a remote specifier that was redirected to
  /// another specifier.
  Redirect {
    from: ModuleSpecifier,
    to: ModuleSpecifier,
  },
  /// An error occurred when resolving the specifier string.
  ResolutionError(deno_graph::ResolutionError),
  /// Invalid `node:` specifier.
  InvalidNodeSpecifier(ModuleSpecifier),
  /// Bare specifier is used for `node:` specifier
  BareNodeSpecifier(String),
}

impl DenoDiagnostic {
  fn code(&self) -> &str {
    match self {
      Self::DenoWarn(_) => "deno-warn",
      Self::ImportMapRemap { .. } => "import-map-remap",
      Self::InvalidAttributeType(_) => "invalid-attribute-type",
      Self::NoAttributeType => "no-attribute-type",
      Self::NoCache(_) => "no-cache",
      Self::NotInstalledJsr(_, _) => "not-installed-jsr",
      Self::NotInstalledNpm(_, _) => "not-installed-npm",
      Self::NoLocal(_) => "no-local",
      Self::Redirect { .. } => "redirect",
      Self::ResolutionError(err) => {
        if graph_util::get_resolution_error_bare_node_specifier(err).is_some() {
          "import-node-prefix-missing"
        } else {
          match err {
            ResolutionError::InvalidDowngrade { .. } => "invalid-downgrade",
            ResolutionError::InvalidJsrHttpsTypesImport { .. } => {
              "invalid-jsr-https-types-import"
            }
            ResolutionError::InvalidLocalImport { .. } => {
              "invalid-local-import"
            }
            ResolutionError::InvalidSpecifier { error, .. } => match error {
              SpecifierError::ImportPrefixMissing { .. } => {
                "import-prefix-missing"
              }
              SpecifierError::InvalidUrl(_) => "invalid-url",
            },
            ResolutionError::ResolverError { .. } => "resolver-error",
          }
        }
      }
      Self::InvalidNodeSpecifier(_) => "resolver-error",
      Self::BareNodeSpecifier(_) => "import-node-prefix-missing",
    }
  }

  /// A "static" method which for a diagnostic that originated from the
  /// structure returns a code action which can resolve the diagnostic.
  pub fn get_code_action(
    specifier: &ModuleSpecifier,
    diagnostic: &lsp::Diagnostic,
  ) -> Result<lsp::CodeAction, AnyError> {
    if let Some(lsp::NumberOrString::String(code)) = &diagnostic.code {
      let code_action = match code.as_str() {
        "import-map-remap" => {
          let data = diagnostic
            .data
            .clone()
            .ok_or_else(|| anyhow!("Diagnostic is missing data"))?;
          let DiagnosticDataImportMapRemap { from, to } =
            serde_json::from_value(data)?;
          lsp::CodeAction {
            title: format!("Update \"{from}\" to \"{to}\" to use import map."),
            kind: Some(lsp::CodeActionKind::QUICKFIX),
            diagnostics: Some(vec![diagnostic.clone()]),
            edit: Some(lsp::WorkspaceEdit {
              changes: Some(HashMap::from([(
                url_to_uri(specifier)?,
                vec![lsp::TextEdit {
                  new_text: format!("\"{to}\""),
                  range: diagnostic.range,
                }],
              )])),
              ..Default::default()
            }),
            ..Default::default()
          }
        }
        "no-attribute-type" => lsp::CodeAction {
          title: "Insert import attribute.".to_string(),
          kind: Some(lsp::CodeActionKind::QUICKFIX),
          diagnostics: Some(vec![diagnostic.clone()]),
          edit: Some(lsp::WorkspaceEdit {
            changes: Some(HashMap::from([(
              url_to_uri(specifier)?,
              vec![lsp::TextEdit {
                new_text: " with { type: \"json\" }".to_string(),
                range: lsp::Range {
                  start: diagnostic.range.end,
                  end: diagnostic.range.end,
                },
              }],
            )])),
            ..Default::default()
          }),
          ..Default::default()
        },
        "no-cache" | "not-installed-jsr" | "not-installed-npm" => {
          let data = diagnostic
            .data
            .clone()
            .ok_or_else(|| anyhow!("Diagnostic is missing data"))?;
          let data: DiagnosticDataSpecifier = serde_json::from_value(data)?;
          let title = if matches!(
            code.as_str(),
            "not-installed-jsr" | "not-installed-npm"
          ) {
            format!("Install \"{}\" and its dependencies.", data.specifier)
          } else {
            format!("Cache \"{}\" and its dependencies.", data.specifier)
          };
          lsp::CodeAction {
            title,
            kind: Some(lsp::CodeActionKind::QUICKFIX),
            diagnostics: Some(vec![diagnostic.clone()]),
            command: Some(lsp::Command {
              title: "".to_string(),
              command: "deno.cache".to_string(),
              arguments: Some(vec![json!([data.specifier]), json!(&specifier)]),
            }),
            ..Default::default()
          }
        }
        "no-local" => {
          let data = diagnostic
            .data
            .clone()
            .ok_or_else(|| anyhow!("Diagnostic is missing data"))?;
          let data: DiagnosticDataNoLocal = serde_json::from_value(data)?;
          lsp::CodeAction {
            title: data.message,
            kind: Some(lsp::CodeActionKind::QUICKFIX),
            diagnostics: Some(vec![diagnostic.clone()]),
            edit: Some(lsp::WorkspaceEdit {
              changes: Some(HashMap::from([(
                url_to_uri(specifier)?,
                vec![lsp::TextEdit {
                  new_text: format!(
                    "\"{}\"",
                    relative_specifier(&data.to, specifier)
                  ),
                  range: diagnostic.range,
                }],
              )])),
              ..Default::default()
            }),
            ..Default::default()
          }
        }
        "redirect" => {
          let data = diagnostic
            .data
            .clone()
            .ok_or_else(|| anyhow!("Diagnostic is missing data"))?;
          let data: DiagnosticDataRedirect = serde_json::from_value(data)?;
          lsp::CodeAction {
            title: "Update specifier to its redirected specifier.".to_string(),
            kind: Some(lsp::CodeActionKind::QUICKFIX),
            diagnostics: Some(vec![diagnostic.clone()]),
            edit: Some(lsp::WorkspaceEdit {
              changes: Some(HashMap::from([(
                url_to_uri(specifier)?,
                vec![lsp::TextEdit {
                  new_text: format!(
                    "\"{}\"",
                    specifier_text_for_redirected(&data.redirect, specifier)
                  ),
                  range: diagnostic.range,
                }],
              )])),
              ..Default::default()
            }),
            ..Default::default()
          }
        }
        "import-node-prefix-missing" => {
          let data = diagnostic
            .data
            .clone()
            .ok_or_else(|| anyhow!("Diagnostic is missing data"))?;
          let data: DiagnosticDataStrSpecifier = serde_json::from_value(data)?;
          lsp::CodeAction {
            title: format!("Update specifier to node:{}", data.specifier),
            kind: Some(lsp::CodeActionKind::QUICKFIX),
            diagnostics: Some(vec![diagnostic.clone()]),
            edit: Some(lsp::WorkspaceEdit {
              changes: Some(HashMap::from([(
                url_to_uri(specifier)?,
                vec![lsp::TextEdit {
                  new_text: format!("\"node:{}\"", data.specifier),
                  range: diagnostic.range,
                }],
              )])),
              ..Default::default()
            }),
            ..Default::default()
          }
        }
        _ => {
          return Err(anyhow!(
            "Unsupported diagnostic code (\"{}\") provided.",
            code
          ))
        }
      };
      Ok(code_action)
    } else {
      Err(anyhow!("Unsupported diagnostic code provided."))
    }
  }

  /// Given a reference to the code from an LSP diagnostic, determine if the
  /// diagnostic is fixable or not
  pub fn is_fixable(diagnostic: &lsp_types::Diagnostic) -> bool {
    if let Some(lsp::NumberOrString::String(code)) = &diagnostic.code {
      match code.as_str() {
        "import-map-remap"
        | "no-cache"
        | "not-installed-jsr"
        | "not-installed-npm"
        | "no-attribute-type"
        | "redirect"
        | "import-node-prefix-missing" => true,
        "no-local" => diagnostic.data.is_some(),
        _ => false,
      }
    } else {
      false
    }
  }

  /// Convert to an lsp Diagnostic when the range the diagnostic applies to is
  /// provided.
  pub fn to_lsp_diagnostic(&self, range: &lsp::Range) -> lsp::Diagnostic {
    fn no_local_message(
      specifier: &ModuleSpecifier,
      maybe_sloppy_resolution: Option<&SloppyImportsResolution>,
    ) -> String {
      let mut message = format!(
        "Unable to load a local module: {}\n",
        to_percent_decoded_str(specifier.as_ref())
      );
      if let Some(res) = maybe_sloppy_resolution {
        message.push_str(&res.as_suggestion_message());
        message.push('.');
      } else {
        message.push_str("Please check the file path.");
      }
      message
    }

    let (severity, message, data) = match self {
      Self::DenoWarn(message) => (lsp::DiagnosticSeverity::WARNING, message.to_string(), None),
      Self::ImportMapRemap { from, to } => (lsp::DiagnosticSeverity::HINT, format!("The import specifier can be remapped to \"{to}\" which will resolve it via the active import map."), Some(json!({ "from": from, "to": to }))),
      Self::InvalidAttributeType(assert_type) => (lsp::DiagnosticSeverity::ERROR, format!("The module is a JSON module and expected an attribute type of \"json\". Instead got \"{assert_type}\"."), None),
      Self::NoAttributeType => (lsp::DiagnosticSeverity::ERROR, "The module is a JSON module and not being imported with an import attribute. Consider adding `with { type: \"json\" }` to the import statement.".to_string(), None),
      Self::NoCache(specifier) => (lsp::DiagnosticSeverity::ERROR, format!("Uncached or missing remote URL: {specifier}"), Some(json!({ "specifier": specifier }))),
      Self::NotInstalledJsr(pkg_req, specifier) => (lsp::DiagnosticSeverity::ERROR, format!("JSR package \"{pkg_req}\" is not installed or doesn't exist."), Some(json!({ "specifier": specifier }))),
      Self::NotInstalledNpm(pkg_req, specifier) => (lsp::DiagnosticSeverity::ERROR, format!("NPM package \"{pkg_req}\" is not installed or doesn't exist."), Some(json!({ "specifier": specifier }))),
      Self::NoLocal(specifier) => {
        let maybe_sloppy_resolution = CliSloppyImportsResolver::new(
          SloppyImportsCachedFs::new(Arc::new(deno_fs::RealFs))
        ).resolve(specifier, SloppyImportsResolutionMode::Execution);
        let data = maybe_sloppy_resolution.as_ref().map(|res| {
          json!({
            "specifier": specifier,
            "to": res.as_specifier(),
            "message": res.as_quick_fix_message(),
          })
        });
        (lsp::DiagnosticSeverity::ERROR, no_local_message(specifier, maybe_sloppy_resolution.as_ref()), data)
      },
      Self::Redirect { from, to} => (lsp::DiagnosticSeverity::INFORMATION, format!("The import of \"{from}\" was redirected to \"{to}\"."), Some(json!({ "specifier": from, "redirect": to }))),
      Self::ResolutionError(err) => {
        let mut message;
        message = enhanced_resolution_error_message(err);
        if let deno_graph::ResolutionError::ResolverError {error, ..} = err{
          if let ResolveError::Other(resolve_error, ..) = (*error).as_ref() {
            if let Some(ImportMapError::UnmappedBareSpecifier(specifier, _)) = resolve_error.downcast_ref::<ImportMapError>() {
              if specifier.chars().next().unwrap_or('\0') == '@'{
                let hint = format!("\nHint: Use [deno add {}] to add the dependency.", specifier);
                message.push_str(hint.as_str());
              }
            }
          }
        }
        (
        lsp::DiagnosticSeverity::ERROR,
        message,
        graph_util::get_resolution_error_bare_node_specifier(err)
          .map(|specifier| json!({ "specifier": specifier }))
      )},
      Self::InvalidNodeSpecifier(specifier) => (lsp::DiagnosticSeverity::ERROR, format!("Unknown Node built-in module: {}", specifier.path()), None),
      Self::BareNodeSpecifier(specifier) => (lsp::DiagnosticSeverity::WARNING, format!("\"{}\" is resolved to \"node:{}\". If you want to use a built-in Node module, add a \"node:\" prefix.", specifier, specifier), Some(json!({ "specifier": specifier }))),
    };
    lsp::Diagnostic {
      range: *range,
      severity: Some(severity),
      code: Some(lsp::NumberOrString::String(self.code().to_string())),
      source: Some(DiagnosticSource::Deno.as_lsp_source().to_string()),
      message,
      data,
      ..Default::default()
    }
  }
}

fn specifier_text_for_redirected(redirect: &Url, referrer: &Url) -> String {
  if redirect.scheme() == "file" && referrer.scheme() == "file" {
    // use a relative specifier when it's going to a file url
    relative_specifier(redirect, referrer)
  } else {
    redirect.to_string()
  }
}

fn relative_specifier(specifier: &Url, referrer: &Url) -> String {
  match referrer.make_relative(specifier) {
    Some(relative) => {
      if relative.starts_with('.') {
        relative
      } else {
        format!("./{}", relative)
      }
    }
    None => specifier.to_string(),
  }
}

fn diagnose_resolution(
  snapshot: &language_server::StateSnapshot,
  dependency_key: &str,
  resolution: &Resolution,
  is_dynamic: bool,
  maybe_assert_type: Option<&str>,
  referrer_doc: &Document,
  import_map: Option<&ImportMap>,
) -> Vec<DenoDiagnostic> {
  fn check_redirect_diagnostic(
    specifier: &ModuleSpecifier,
    doc: &Document,
  ) -> Option<DenoDiagnostic> {
    let doc_specifier = doc.specifier();
    // If the module was redirected, we want to issue an informational
    // diagnostic that indicates this. This then allows us to issue a code
    // action to replace the specifier with the final redirected one.
    if specifier.scheme() == "jsr" || doc_specifier == specifier {
      return None;
    }
    // don't bother warning about sloppy import redirects from .js to .d.ts
    // because explaining how to fix this via a diagnostic involves using
    // @deno-types and that's a bit complicated to explain
    let is_sloppy_import_dts_redirect = doc_specifier.scheme() == "file"
      && doc.media_type().is_declaration()
      && !MediaType::from_specifier(specifier).is_declaration();
    if is_sloppy_import_dts_redirect {
      return None;
    }

    Some(DenoDiagnostic::Redirect {
      from: specifier.clone(),
      to: doc_specifier.clone(),
    })
  }

  let mut diagnostics = vec![];
  match resolution {
    Resolution::Ok(resolved) => {
      let file_referrer = referrer_doc.file_referrer();
      let specifier = &resolved.specifier;
      let managed_npm_resolver =
        snapshot.resolver.maybe_managed_npm_resolver(file_referrer);
      for (_, headers) in snapshot
        .resolver
        .redirect_chain_headers(specifier, file_referrer)
      {
        if let Some(message) = headers.get("x-deno-warning") {
          diagnostics.push(DenoDiagnostic::DenoWarn(message.clone()));
        }
      }
      if let Some(doc) =
        snapshot.documents.get_or_load(specifier, file_referrer)
      {
        if let Some(headers) = doc.maybe_headers() {
          if let Some(message) = headers.get("x-deno-warning") {
            diagnostics.push(DenoDiagnostic::DenoWarn(message.clone()));
          }
        }
        if let Some(diagnostic) = check_redirect_diagnostic(specifier, &doc) {
          diagnostics.push(diagnostic);
        }
        if doc.media_type() == MediaType::Json {
          match maybe_assert_type {
            // The module has the correct assertion type, no diagnostic
            Some("json") => (),
            // The dynamic import statement is missing an attribute type, which
            // we might not be able to statically detect, therefore we will
            // not provide a potentially incorrect diagnostic.
            None if is_dynamic => (),
            // The module has an incorrect assertion type, diagnostic
            Some(assert_type) => diagnostics.push(
              DenoDiagnostic::InvalidAttributeType(assert_type.to_string()),
            ),
            // The module is missing an attribute type, diagnostic
            None => diagnostics.push(DenoDiagnostic::NoAttributeType),
          }
        }
      } else if let Ok(pkg_ref) =
        JsrPackageReqReference::from_specifier(specifier)
      {
        let req = pkg_ref.into_inner().req;
        diagnostics
          .push(DenoDiagnostic::NotInstalledJsr(req, specifier.clone()));
      } else if let Ok(pkg_ref) =
        NpmPackageReqReference::from_specifier(specifier)
      {
        if let Some(npm_resolver) = managed_npm_resolver {
          // show diagnostics for npm package references that aren't cached
          let req = pkg_ref.into_inner().req;
          if !npm_resolver.is_pkg_req_folder_cached(&req) {
            diagnostics
              .push(DenoDiagnostic::NotInstalledNpm(req, specifier.clone()));
          }
        }
      } else if let Some(module_name) = specifier.as_str().strip_prefix("node:")
      {
        if !deno_node::is_builtin_node_module(module_name) {
          diagnostics
            .push(DenoDiagnostic::InvalidNodeSpecifier(specifier.clone()));
        } else if module_name == dependency_key {
          let mut is_mapped = false;
          if let Some(import_map) = import_map {
            if let Resolution::Ok(resolved) = &resolution {
              if import_map.resolve(module_name, &resolved.specifier).is_ok() {
                is_mapped = true;
              }
            }
          }
          // show diagnostics for bare node specifiers that aren't mapped by import map
          if !is_mapped {
            diagnostics
              .push(DenoDiagnostic::BareNodeSpecifier(module_name.to_string()));
          }
        } else if let Some(npm_resolver) = managed_npm_resolver {
          // check that a @types/node package exists in the resolver
          let types_node_req = PackageReq::from_str("@types/node").unwrap();
          if !npm_resolver.is_pkg_req_folder_cached(&types_node_req) {
            diagnostics.push(DenoDiagnostic::NotInstalledNpm(
              types_node_req,
              ModuleSpecifier::parse("npm:@types/node").unwrap(),
            ));
          }
        }
      } else {
        // When the document is not available, it means that it cannot be found
        // in the cache or locally on the disk, so we want to issue a diagnostic
        // about that.
        let deno_diagnostic = match specifier.scheme() {
          "file" => DenoDiagnostic::NoLocal(specifier.clone()),
          _ => DenoDiagnostic::NoCache(specifier.clone()),
        };
        diagnostics.push(deno_diagnostic);
      }
    }
    // The specifier resolution resulted in an error, so we want to issue a
    // diagnostic for that.
    Resolution::Err(err) => {
      diagnostics.push(DenoDiagnostic::ResolutionError(*err.clone()))
    }
    _ => (),
  }
  diagnostics
}

/// Generate diagnostics related to a dependency. The dependency is analyzed to
/// determine if it can be remapped to the active import map as well as surface
/// any diagnostics related to the resolved code or type dependency.
fn diagnose_dependency(
  diagnostics: &mut Vec<lsp::Diagnostic>,
  snapshot: &language_server::StateSnapshot,
  referrer_doc: &Document,
  dependency_key: &str,
  dependency: &deno_graph::Dependency,
) {
  let referrer = referrer_doc.specifier();
  if snapshot.resolver.in_node_modules(referrer) {
    return; // ignore, surface typescript errors instead
  }

  let import_map = snapshot
    .config
    .tree
    .data_for_specifier(referrer_doc.file_referrer().unwrap_or(referrer))
    .and_then(|d| d.resolver.maybe_import_map());
  if let Some(import_map) = import_map {
    if let Resolution::Ok(resolved) = &dependency.maybe_code {
      if let Some(to) = import_map.lookup(&resolved.specifier, referrer) {
        if dependency_key != to {
          diagnostics.push(
            DenoDiagnostic::ImportMapRemap {
              from: dependency_key.to_string(),
              to,
            }
            .to_lsp_diagnostic(&documents::to_lsp_range(&resolved.range)),
          );
        }
      }
    }
  }

  let import_ranges: Vec<_> = dependency
    .imports
    .iter()
    .map(|i| documents::to_lsp_range(&i.specifier_range))
    .collect();
  // TODO(nayeemrmn): This is a crude way of detecting `@deno-types` which has
  // a different specifier and therefore needs a separate call to
  // `diagnose_resolution()`. It would be much cleaner if that were modelled as
  // a separate dependency: https://github.com/denoland/deno_graph/issues/247.
  let is_types_deno_types = !dependency.maybe_type.is_none()
    && !dependency.imports.iter().any(|i| {
      dependency
        .maybe_type
        .includes(&i.specifier_range.start)
        .is_some()
    });

  diagnostics.extend(
    diagnose_resolution(
      snapshot,
      dependency_key,
      if dependency.maybe_code.is_none()
        // If not @deno-types, diagnose the types if the code errored because
        // it's likely resolving into the node_modules folder, which might be
        // erroring correctly due to resolution only being for bundlers. Let this
        // fail at runtime if necessary, but don't bother erroring in the editor
        || !is_types_deno_types && matches!(dependency.maybe_type, Resolution::Ok(_))
          && matches!(dependency.maybe_code, Resolution::Err(_))
      {
        &dependency.maybe_type
      } else {
        &dependency.maybe_code
      },
      dependency.is_dynamic,
      dependency.maybe_attribute_type.as_deref(),
      referrer_doc,
      import_map,
    )
    .iter()
    .flat_map(|diag| {
      import_ranges
        .iter()
        .map(|range| diag.to_lsp_diagnostic(range))
    }),
  );

  if is_types_deno_types {
    let range = match &dependency.maybe_type {
      Resolution::Ok(resolved) => documents::to_lsp_range(&resolved.range),
      Resolution::Err(error) => documents::to_lsp_range(error.range()),
      Resolution::None => unreachable!(),
    };
    diagnostics.extend(
      diagnose_resolution(
        snapshot,
        dependency_key,
        &dependency.maybe_type,
        dependency.is_dynamic,
        dependency.maybe_attribute_type.as_deref(),
        referrer_doc,
        import_map,
      )
      .iter()
      .map(|diag| diag.to_lsp_diagnostic(&range)),
    );
  }
}

/// Generate diagnostics that come from Deno module resolution logic (like
/// dependencies) or other Deno specific diagnostics, like the ability to use
/// an import map to shorten an URL.
fn generate_deno_diagnostics(
  snapshot: &language_server::StateSnapshot,
  config: &Config,
  token: CancellationToken,
) -> DiagnosticVec {
  let mut diagnostics_vec = Vec::new();

  for document in snapshot
    .documents
    .documents(DocumentsFilter::OpenDiagnosable)
  {
    if token.is_cancelled() {
      break;
    }
    let mut diagnostics = Vec::new();
    let specifier = document.specifier();
    if config.specifier_enabled(specifier) {
      for (dependency_key, dependency) in document.dependencies() {
        diagnose_dependency(
          &mut diagnostics,
          snapshot,
          &document,
          dependency_key,
          dependency,
        );
      }
    }
    diagnostics_vec.push(DiagnosticRecord {
      specifier: specifier.clone(),
      versioned: VersionedDiagnostics {
        version: document.maybe_lsp_version(),
        diagnostics,
      },
    });
  }

  diagnostics_vec
}

#[cfg(test)]
mod tests {
  use super::*;
  use crate::lsp::cache::LspCache;
  use crate::lsp::config::Config;
  use crate::lsp::config::Settings;
  use crate::lsp::config::WorkspaceSettings;
  use crate::lsp::documents::Documents;
  use crate::lsp::documents::LanguageId;
  use crate::lsp::language_server::StateSnapshot;
  use crate::lsp::resolver::LspResolver;

  use deno_config::deno_json::ConfigFile;
  use pretty_assertions::assert_eq;
  use std::sync::Arc;
  use test_util::TempDir;

  fn mock_config() -> Config {
    let root_url = resolve_url("file:///").unwrap();
    let root_uri = url_to_uri(&root_url).unwrap();
    Config {
      settings: Arc::new(Settings {
        unscoped: Arc::new(WorkspaceSettings {
          enable: Some(true),
          lint: true,
          ..Default::default()
        }),
        ..Default::default()
      }),
      workspace_folders: Arc::new(vec![(
        root_url,
        lsp::WorkspaceFolder {
          uri: root_uri,
          name: "".to_string(),
        },
      )]),
      ..Default::default()
    }
  }

  async fn setup(
    sources: &[(&str, &str, i32, LanguageId)],
    maybe_import_map: Option<(&str, &str)>,
  ) -> (TempDir, StateSnapshot) {
    let temp_dir = TempDir::new();
    let root_uri = temp_dir.url();
    let cache = LspCache::new(Some(root_uri.join(".deno_dir").unwrap()));
    let mut config = Config::new_with_roots([root_uri.clone()]);
    if let Some((relative_path, json_string)) = maybe_import_map {
      let base_url = root_uri.join(relative_path).unwrap();
      let config_file = ConfigFile::new(
        json_string,
        base_url,
        &deno_config::deno_json::ConfigParseOptions::default(),
      )
      .unwrap();
      config.tree.inject_config_file(config_file).await;
    }
    let resolver =
      Arc::new(LspResolver::from_config(&config, &cache, None).await);
    let mut documents = Documents::default();
    documents.update_config(&config, &resolver, &cache, &Default::default());
    for (relative_path, source, version, language_id) in sources {
      let specifier = root_uri.join(relative_path).unwrap();
      documents.open(
        specifier.clone(),
        *version,
        *language_id,
        (*source).into(),
        None,
      );
    }
    (
      temp_dir,
      StateSnapshot {
        project_version: 0,
        documents: Arc::new(documents),
        assets: Default::default(),
        config: Arc::new(config),
        resolver,
      },
    )
  }

  #[tokio::test]
  async fn test_enabled_then_disabled_specifier() {
    let (_, snapshot) = setup(
      &[(
        "a.ts",
        r#"import * as b from "./b.ts";
let a: any = "a";
let c: number = "a";
"#,
        1,
        LanguageId::TypeScript,
      )],
      None,
    )
    .await;
    let snapshot = Arc::new(snapshot);
    let ts_server = TsServer::new(Default::default());
    ts_server.start(None).unwrap();

    // test enabled
    {
      let enabled_config = mock_config();
      let diagnostics = generate_lint_diagnostics(
        &snapshot,
        &enabled_config,
        Default::default(),
      );
      assert_eq!(get_diagnostics_for_single(diagnostics).len(), 6);
      let diagnostics = generate_ts_diagnostics(
        snapshot.clone(),
        &enabled_config,
        &ts_server,
        Default::default(),
      )
      .await
      .unwrap();
      assert_eq!(get_diagnostics_for_single(diagnostics).len(), 4);
      let diagnostics = generate_deno_diagnostics(
        &snapshot,
        &enabled_config,
        Default::default(),
      );
      assert_eq!(get_diagnostics_for_single(diagnostics).len(), 1);
    }

    // now test disabled specifier
    {
      let mut disabled_config = mock_config();
      disabled_config.set_workspace_settings(
        WorkspaceSettings {
          enable: Some(false),
          ..Default::default()
        },
        vec![],
      );

      let diagnostics = generate_lint_diagnostics(
        &snapshot,
        &disabled_config,
        Default::default(),
      );
      assert_eq!(get_diagnostics_for_single(diagnostics).len(), 0);
      let diagnostics = generate_ts_diagnostics(
        snapshot.clone(),
        &disabled_config,
        &ts_server,
        Default::default(),
      )
      .await
      .unwrap();
      assert_eq!(get_diagnostics_for_single(diagnostics).len(), 0);
      let diagnostics = generate_deno_diagnostics(
        &snapshot,
        &disabled_config,
        Default::default(),
      );
      assert_eq!(get_diagnostics_for_single(diagnostics).len(), 0);
    }
  }

  fn get_diagnostics_for_single(
    diagnostic_vec: DiagnosticVec,
  ) -> Vec<lsp::Diagnostic> {
    if diagnostic_vec.is_empty() {
      return vec![];
    }
    assert_eq!(diagnostic_vec.len(), 1);
    diagnostic_vec
      .into_iter()
      .next()
      .unwrap()
      .versioned
      .diagnostics
  }

  #[tokio::test]
  async fn test_deno_diagnostics_with_import_map() {
    let (temp_dir, snapshot) = setup(
      &[
        (
          "std/assert/mod.ts",
          "export function assert() {}",
          1,
          LanguageId::TypeScript,
        ),
        (
          "a/file.ts",
          "import { assert } from \"../std/assert/mod.ts\";\n\nassert();\n",
          1,
          LanguageId::TypeScript,
        ),
      ],
      Some((
        "a/deno.json",
        r#"{
        "imports": {
          "/~/std/": "../std/"
        }
      }"#,
      )),
    )
    .await;
    let config = mock_config();
    let token = CancellationToken::new();
    let actual = generate_deno_diagnostics(&snapshot, &config, token);
    assert_eq!(actual.len(), 2);
    for record in actual {
      let relative_specifier =
        temp_dir.url().make_relative(&record.specifier).unwrap();
      match relative_specifier.as_str() {
        "std/assert/mod.ts" => {
          assert_eq!(json!(record.versioned.diagnostics), json!([]))
        }
        "a/file.ts" => assert_eq!(
          json!(record.versioned.diagnostics),
          json!([
            {
              "range": {
                "start": {
                  "line": 0,
                  "character": 23
                },
                "end": {
                  "line": 0,
                  "character": 45
                }
              },
              "severity": 4,
              "code": "import-map-remap",
              "source": "deno",
              "message": "The import specifier can be remapped to \"/~/std/assert/mod.ts\" which will resolve it via the active import map.",
              "data": {
                "from": "../std/assert/mod.ts",
                "to": "/~/std/assert/mod.ts"
              }
            }
          ])
        ),
        _ => unreachable!("unexpected specifier {}", record.specifier),
      }
    }
  }

  #[test]
  fn test_get_code_action_import_map_remap() {
    let specifier = ModuleSpecifier::parse("file:///a/file.ts").unwrap();
    let result = DenoDiagnostic::get_code_action(&specifier, &lsp::Diagnostic {
      range: lsp::Range {
        start: lsp::Position { line: 0, character: 23 },
        end: lsp::Position { line: 0, character: 50 },
      },
      severity: Some(lsp::DiagnosticSeverity::HINT),
      code: Some(lsp::NumberOrString::String("import-map-remap".to_string())),
      source: Some("deno".to_string()),
      message: "The import specifier can be remapped to \"/~/std/assert/mod.ts\" which will resolve it via the active import map.".to_string(),
      data: Some(json!({
        "from": "../std/assert/mod.ts",
        "to": "/~/std/assert/mod.ts"
      })),
      ..Default::default()
    });
    assert!(result.is_ok());
    let actual = result.unwrap();
    assert_eq!(
      json!(actual),
      json!({
        "title": "Update \"../std/assert/mod.ts\" to \"/~/std/assert/mod.ts\" to use import map.",
        "kind": "quickfix",
        "diagnostics": [
          {
            "range": {
              "start": {
                "line": 0,
                "character": 23
              },
              "end": {
                "line": 0,
                "character": 50
              }
            },
            "severity": 4,
            "code": "import-map-remap",
            "source": "deno",
            "message": "The import specifier can be remapped to \"/~/std/assert/mod.ts\" which will resolve it via the active import map.",
            "data": {
              "from": "../std/assert/mod.ts",
              "to": "/~/std/assert/mod.ts"
            }
          }
        ],
        "edit": {
          "changes": {
            "file:///a/file.ts": [
              {
                "range": {
                  "start": {
                    "line": 0,
                    "character": 23
                  },
                  "end": {
                    "line": 0,
                    "character": 50
                  }
                },
                "newText": "\"/~/std/assert/mod.ts\""
              }
            ]
          }
        }
      })
    );
  }

  #[tokio::test]
  async fn duplicate_diagnostics_for_duplicate_imports() {
    let (_, snapshot) = setup(
      &[(
        "a.ts",
        r#"
        // @deno-types="bad.d.ts"
        import "bad.js";
        import "bad.js";
        "#,
        1,
        LanguageId::TypeScript,
      )],
      None,
    )
    .await;
    let config = mock_config();
    let token = CancellationToken::new();
    let actual = generate_deno_diagnostics(&snapshot, &config, token);
    assert_eq!(actual.len(), 1);
    let record = actual.first().unwrap();
    assert_eq!(
      json!(record.versioned.diagnostics),
      json!([
        {
          "range": {
            "start": {
              "line": 2,
              "character": 15
            },
            "end": {
              "line": 2,
              "character": 23
            }
          },
          "severity": 1,
          "code": "import-prefix-missing",
          "source": "deno",
          "message": "Relative import path \"bad.js\" not prefixed with / or ./ or ../",
        },
        {
          "range": {
            "start": {
              "line": 3,
              "character": 15
            },
            "end": {
              "line": 3,
              "character": 23
            }
          },
          "severity": 1,
          "code": "import-prefix-missing",
          "source": "deno",
          "message": "Relative import path \"bad.js\" not prefixed with / or ./ or ../",
        },
        {
          "range": {
            "start": {
              "line": 1,
              "character": 23
            },
            "end": {
              "line": 1,
              "character": 33
            }
          },
          "severity": 1,
          "code": "import-prefix-missing",
          "source": "deno",
          "message": "Relative import path \"bad.d.ts\" not prefixed with / or ./ or ../",
        },
      ])
    );
  }

  #[tokio::test]
  async fn unable_to_load_a_local_module() {
    let (temp_dir, snapshot) = setup(
      &[(
        "a.ts",
        r#"
        import { 東京 } from "./🦕.ts";
        "#,
        1,
        LanguageId::TypeScript,
      )],
      None,
    )
    .await;
    let config = mock_config();
    let token = CancellationToken::new();
    let actual = generate_deno_diagnostics(&snapshot, &config, token);
    assert_eq!(actual.len(), 1);
    let record = actual.first().unwrap();
    assert_eq!(
      json!(record.versioned.diagnostics),
      json!([
        {
          "range": {
            "start": {
              "line": 1,
              "character": 27
            },
            "end": {
              "line": 1,
              "character": 35
            }
          },
          "severity": 1,
          "code": "no-local",
          "source": "deno",
          "message": format!(
            "Unable to load a local module: {}🦕.ts\nPlease check the file path.",
            temp_dir.url(),
          ),
        }
      ])
    );
  }

  #[test]
  fn test_specifier_text_for_redirected() {
    #[track_caller]
    fn run_test(specifier: &str, referrer: &str, expected: &str) {
      let result = specifier_text_for_redirected(
        &ModuleSpecifier::parse(specifier).unwrap(),
        &ModuleSpecifier::parse(referrer).unwrap(),
      );
      assert_eq!(result, expected);
    }

    run_test("file:///a/a.ts", "file:///a/mod.ts", "./a.ts");
    run_test("file:///a/a.ts", "file:///a/sub_dir/mod.ts", "../a.ts");
    run_test(
      "file:///a/sub_dir/a.ts",
      "file:///a/mod.ts",
      "./sub_dir/a.ts",
    );
    run_test(
      "https://deno.land/x/example/mod.ts",
      "file:///a/sub_dir/a.ts",
      "https://deno.land/x/example/mod.ts",
    );
  }
}