Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/runtime/check.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,8 @@ for (const log of result.logs) {
/app/src/index.ts:3: TS2322: Type 'string' is not assignable to type 'number'.
```

If an `onLoad` [plugin](/bundler/plugins) loads a file, Bun checks the text of that file in [`files`](/bundler#files), or else on disk, not the text that the plugin returns. If the file is in neither place, Bun checks what the plugin returns.

## If `package.json` has a `check` script

Your script wins. `bun check` runs it, like `bun run check`, and passes it your flags. To run the type checker in that project, pass `--no-script`.
Expand Down
3 changes: 3 additions & 0 deletions packages/bun-types/bun.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3516,6 +3516,9 @@ declare module "bun" {
* from the file that `tsconfig` names. The check also uses the `conditions`
* and the `loader` of the build.
*
* If an `onLoad` plugin loads a file, the check reads the text of that file
* from `files`, or else from disk, not what the plugin returns.
*
* @default false
*
* @example
Expand Down
45 changes: 36 additions & 9 deletions src/bundler/bundle_v2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2537,7 +2537,7 @@ pub mod bv2_impl {
}

/// `BundleOptions::type_check`. Every file is parsed, so the pool is idle and the text of
/// each file is in the graph.
/// each file is in the graph or in `file_map`.
fn type_check(&self) -> Result<(), Error> {
let Some(type_check) = self.transpiler.options.type_check else {
return Ok(());
Expand Down Expand Up @@ -2583,14 +2583,41 @@ pub mod bv2_impl {
.map(|index| sources[index].path.text);
// The types are those of what is written, as in `bun check`, not of what a plugin makes
// of it. What only a plugin provides is written nowhere else.
let mut sources = (sources.iter().zip(loaders).zip(flags))
.filter(|((source, loader), flags)| {
loader.is_javascript_like_or_json()
&& source.path.is_file()
&& !(flags.contains(crate::Graph::InputFileFlags::IS_LOADED_BY_PLUGIN)
&& bun_sys::exists(source.path.text))
})
.map(|((source, loader), _)| (source.path.text, source.contents(), *loader));
let file_map = self.file_map;
let by_name = &self.transpiler.options.loaders;
let is_code = Loader::is_javascript_like_or_json;
let checked_source = move |index: usize| {
let (source, loader) = (&sources[index], loaders[index]);
let path = &source.path;
if !path.is_file() {
return None;
}
if !is_loaded_by_plugin(index) {
return is_code(loader).then(|| (path.text, source.contents(), loader));
}
// `files` has what is written. The loader in the graph is the plugin's.
if let Some(text) = file_map.and_then(|files| files.get(path.text)) {
let loader = path.loader(by_name)?;
return is_code(loader).then_some((path.text, text, loader));
}
Comment thread
robobun marked this conversation as resolved.
Resolved
let is_only_of_plugin = is_code(loader) && !bun_sys::exists(path.text);
is_only_of_plugin.then(|| (path.text, source.contents(), loader))
};
// A file of `files` that only types use is not in the graph.
let graphs = &self.graph.build_graphs;
let entries = file_map.into_iter().flat_map(|files| files.map.iter());
let not_loaded = entries.filter_map(move |(key, text)| {
let (key, text): (&[u8], &[u8]) = (key, text);
let loader = Fs::Path::init(key).loader(by_name)?;
let mut rows = graphs.values().filter_map(|graph| graph.get(key));
// The disk keeps a file that it has too: a row can be that file by another name.
let is_given = loader.is_javascript_like()
Comment thread
robobun marked this conversation as resolved.
Resolved
&& bun_paths::is_absolute_loose(key)
&& !rows.any(|index| checked_source(index as usize).is_some())
&& !bun_sys::exists(key);
Comment thread
robobun marked this conversation as resolved.
Resolved
is_given.then_some((key, text, loader))
});
let mut sources = not_loaded.chain((0..sources.len()).filter_map(checked_source));
let checked = options::TypeChecked {
cwd: self.transpiler.fs().top_level_dir(),
tsconfig: self.transpiler.options.tsconfig_override.as_deref(),
Expand Down
3 changes: 2 additions & 1 deletion src/bundler/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1188,13 +1188,14 @@ pub struct TypeChecked<'a, 'i> {
pub tsconfig: Option<&'a [u8]>,
/// `custom_conditions`
pub conditions: &'a [Box<[u8]>],
/// `loaders`. A file that only has types is not in the bundle, so not among `sources`.
/// `loaders`. A file on disk that only has types is not in the bundle, so not among `sources`.
pub loaders: &'a LoaderHashTable,
/// The path of each JavaScript or TypeScript file that an entry point resolved to, or that a
/// page imports.
pub entry_points: &'i mut dyn Iterator<Item = &'a [u8]>,
/// The path, the text and the loader of each JavaScript, TypeScript and JSON file of the
/// bundle, which the type checker takes instead of reading the file again.
/// Also each file of `files` that is in neither the bundle nor on disk.
pub sources: &'i mut dyn Iterator<Item = (&'a [u8], &'a [u8], Loader)>,
}

Expand Down
Loading
Loading