Unverified Commit 7c366e61 authored by Jimmy Miller's avatar Jimmy Miller Committed by GitHub

[turbopack] Track webpack loader build dependency files (#98777)

Track exact existing files and warn when loaders register unsupported
build dependency inputs.

I'm going to take this bit by bit so it is nice and reviewable. Does add
some noise. But the final fully supported thing was just too large. So I
will keep removing error cases on each commit.
parent e648495f
import { utilFn } from '../../utils/unsupported-build-dependency'
export default function Page() {
return <p>{utilFn()}</p>
}
...@@ -4,6 +4,14 @@ const fs = require('node:fs') ...@@ -4,6 +4,14 @@ const fs = require('node:fs')
const loader = async function (content) { const loader = async function (content) {
this.async() this.async()
if (this.resourcePath.endsWith('unsupported-build-dependency.ts')) {
this.addBuildDependency(path.join(__dirname, 'utils'))
return this.callback(
null,
`export const utilFn = () => 'unsupported build dependency';`
)
}
if (!this.resourcePath.endsWith('file-to-transform.ts')) { if (!this.resourcePath.endsWith('file-to-transform.ts')) {
return this.callback(null, content) return this.callback(null, content)
} }
...@@ -15,10 +23,15 @@ const loader = async function (content) { ...@@ -15,10 +23,15 @@ const loader = async function (content) {
this.addDependency(result) this.addDependency(result)
const missingDependency = path.join(context, 'missing-dependency.ts') const missingDependency = path.join(context, 'missing-dependency.ts')
this.addMissingDependency(missingDependency) this.addMissingDependency(missingDependency)
const buildDependency = path.join(context, 'build-dependency.js')
this.addBuildDependency(buildDependency)
const buildDependencyValue = fs
.readFileSync(buildDependency, 'utf8')
.match(/'([^']+)'/)[1]
this.callback( this.callback(
null, null,
`export const utilFn = () => 'Generated at ${new Date().toISOString()}, missing dependency: ${fs.existsSync(missingDependency)}';` `export const utilFn = () => 'Generated at ${new Date().toISOString()}, missing dependency: ${fs.existsSync(missingDependency)}, build dependency: ${buildDependencyValue}';`
) )
} }
......
...@@ -39,4 +39,34 @@ describe('turbopack-loader-file-dependencies', () => { ...@@ -39,4 +39,34 @@ describe('turbopack-loader-file-dependencies', () => {
} }
) )
}) })
it('should update when a build dependency changes', async () => {
const $ = await next.render$('/')
expect($('p').text()).toContain('build dependency: build-one')
await next.patchFile(
'utils/build-dependency.js',
"module.exports = 'build-two'",
async () => {
await retry(async () => {
const $2 = await next.render$('/')
expect($2('p').text()).toContain('build dependency: build-two')
}, 10000)
}
)
})
// @force-gate turbopack
it('warns for unsupported build dependency inputs', async () => {
const outputIndex = next.cliOutput.length
const $ = await next.render$('/unsupported')
expect($('p').text()).toContain('unsupported build dependency')
await retry(() => {
const output = next.cliOutput.slice(outputIndex)
expect(output).toContain('Unsupported webpack loader build dependency')
expect(output).toContain('/utils')
expect(output).toContain('exact existing file')
expect(output).not.toMatch(/EISDIR|ELOOP/)
})
})
}) })
...@@ -173,6 +173,7 @@ const transform = ( ...@@ -173,6 +173,7 @@ const transform = (
const loadersWithOptions = loaders.map((loader) => const loadersWithOptions = loaders.map((loader) =>
typeof loader === 'string' ? { loader, options: {} } : loader typeof loader === 'string' ? { loader, options: {} } : loader
) )
const buildDependencies = new Set<string>()
const logs: Array<{ const logs: Array<{
time: number time: number
...@@ -202,6 +203,9 @@ const transform = ( ...@@ -202,6 +203,9 @@ const transform = (
? entry.options ? entry.options
: {} : {}
}, },
addBuildDependency(dependency: string) {
buildDependencies.add(pathResolve(contextDir, dependency))
},
fs: { fs: {
readFile(p: string, optionsOrCb: any, maybeCb: any) { readFile(p: string, optionsOrCb: any, maybeCb: any) {
ipc ipc
...@@ -562,6 +566,7 @@ const transform = ( ...@@ -562,6 +566,7 @@ const transform = (
toPath(dep), toPath(dep),
'**', '**',
]), ]),
buildFilePaths: [...buildDependencies].map(toPath).sort(),
}) })
if (err) { if (err) {
// Resolve loader paths to include in the error message using // Resolve loader paths to include in the error message using
......
...@@ -17,7 +17,7 @@ use turbo_tasks::{ ...@@ -17,7 +17,7 @@ use turbo_tasks::{
}; };
use turbo_tasks_env::ProcessEnv; use turbo_tasks_env::ProcessEnv;
use turbo_tasks_fs::{ use turbo_tasks_fs::{
File, FileContent, FileSystemPath, File, FileContent, FileSystemEntryType, FileSystemPath,
glob::{Glob, GlobOptions}, glob::{Glob, GlobOptions},
json::parse_json_with_source_context, json::parse_json_with_source_context,
rope::Rope, rope::Rope,
...@@ -228,6 +228,36 @@ async fn webpack_loaders_executor( ...@@ -228,6 +228,36 @@ async fn webpack_loaders_executor(
)) ))
} }
#[turbo_tasks::function]
async fn build_files_changed(
cwd: FileSystemPath,
paths: Vec<RcStr>,
source: ResolvedVc<Box<dyn Source>>,
) -> Result<Vc<Completion>> {
for path in paths {
let path = cwd.join(&path)?;
let entry_type = path.get_type().await?;
match &*entry_type {
FileSystemEntryType::File | FileSystemEntryType::NotFound => {
path.read().await?;
}
FileSystemEntryType::Directory
| FileSystemEntryType::Symlink
| FileSystemEntryType::Other
| FileSystemEntryType::Error => {}
}
if !matches!(&*entry_type, FileSystemEntryType::File) {
BuildDependencyIssue {
source: IssueSource::from_source_only(source),
path,
}
.resolved_cell()
.emit();
}
}
Ok(Completion::new())
}
#[turbo_tasks::value_impl] #[turbo_tasks::value_impl]
impl WebpackLoadersProcessedAsset { impl WebpackLoadersProcessedAsset {
#[turbo_tasks::function] #[turbo_tasks::function]
...@@ -632,6 +662,15 @@ impl EvaluateContext for WebpackLoaderContext { ...@@ -632,6 +662,15 @@ impl EvaluateContext for WebpackLoaderContext {
.iter() .iter()
.map(async |p| self.cwd.join(p)?.read().await) .map(async |p| self.cwd.join(p)?.read().await)
.try_join(); .try_join();
let build_file_subscriptions = async {
build_files_changed(
self.cwd.clone(),
build_file_paths,
*self.context_source_for_issue,
)
.await?;
Ok::<_, anyhow::Error>(())
};
let directory_subscriptions = directories let directory_subscriptions = directories
.iter() .iter()
.map(async |(dir, glob)| { .map(async |(dir, glob)| {
...@@ -644,18 +683,9 @@ impl EvaluateContext for WebpackLoaderContext { ...@@ -644,18 +683,9 @@ impl EvaluateContext for WebpackLoaderContext {
try_join!( try_join!(
env_subscriptions, env_subscriptions,
file_subscriptions, file_subscriptions,
build_file_subscriptions,
directory_subscriptions directory_subscriptions
)?; )?;
for build_path in build_file_paths {
let build_path = self.cwd.join(&build_path)?;
BuildDependencyIssue {
source: IssueSource::from_source_only(self.context_source_for_issue),
path: build_path,
}
.resolved_cell()
.emit();
}
} }
} }
InfoMessage::EmittedError { error, severity } => { InfoMessage::EmittedError { error, severity } => {
...@@ -994,7 +1024,7 @@ impl Issue for BuildDependencyIssue { ...@@ -994,7 +1024,7 @@ impl Issue for BuildDependencyIssue {
async fn title(&self) -> Result<StyledString> { async fn title(&self) -> Result<StyledString> {
Ok(StyledString::Text(rcstr!( Ok(StyledString::Text(rcstr!(
"Build dependencies are not yet supported" "Unsupported webpack loader build dependency"
))) )))
} }
...@@ -1008,12 +1038,12 @@ impl Issue for BuildDependencyIssue { ...@@ -1008,12 +1038,12 @@ impl Issue for BuildDependencyIssue {
async fn description(&self) -> Result<Option<StyledString>> { async fn description(&self) -> Result<Option<StyledString>> {
Ok(Some(StyledString::Line(vec![ Ok(Some(StyledString::Line(vec![
StyledString::Text(rcstr!("The file at ")), StyledString::Text(rcstr!("The path at ")),
StyledString::Code(self.path.to_string().into()), StyledString::Code(self.path.to_string().into()),
StyledString::Text( StyledString::Text(
" is a build dependency, which is not yet implemented. " was passed to this.addBuildDependency, but it is not an exact existing file. \
Changing this file or any dependency will not be recognized and might require restarting the \ Only exact existing file paths are supported; other inputs may require \
server" restarting the development server."
.into(), .into(),
), ),
]))) ])))
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment