Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(turbopack): add support for polling file watcher #69684

Merged
merged 5 commits into from
Oct 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
36 changes: 29 additions & 7 deletions crates/napi/src/next_api/project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
entrypoints::Entrypoints,
project::{
DefineEnv, DraftModeOptions, Instrumentation, Middleware, PartialProjectOptions, Project,
ProjectContainer, ProjectOptions,
ProjectContainer, ProjectOptions, WatchOptions,
},
route::{Endpoint, Route},
};
Expand Down Expand Up @@ -78,6 +78,16 @@
}
}

#[napi(object)]
pub struct NapiWatchOptions {
/// Whether to watch the filesystem for file changes.
pub enable: bool,

/// Enable polling at a certain interval if the native file watching doesn't work (e.g.
/// docker).
pub poll_interval_ms: Option<f64>,
}

#[napi(object)]
pub struct NapiProjectOptions {
/// A root path from which all files must be nested under. Trying to access
Expand All @@ -91,8 +101,8 @@
/// deserializing next.config, so passing it as separate option.
pub dist_dir: Option<String>,

/// Whether to watch he filesystem for file changes.
pub watch: bool,
/// Filesystem watcher options.
pub watch: NapiWatchOptions,

/// The contents of next.config.js, serialized to JSON.
pub next_config: String,
Expand Down Expand Up @@ -137,8 +147,8 @@
/// deserializing next.config, so passing it as separate option.
pub dist_dir: Option<Option<String>>,

/// Whether to watch he filesystem for file changes.
pub watch: Option<bool>,
/// Filesystem watcher options.
pub watch: Option<NapiWatchOptions>,

/// The contents of next.config.js, serialized to JSON.
pub next_config: Option<String>,
Expand Down Expand Up @@ -183,12 +193,24 @@
pub memory_limit: Option<f64>,
}

impl From<NapiWatchOptions> for WatchOptions {
fn from(val: NapiWatchOptions) -> Self {
WatchOptions {
enable: val.enable,
poll_interval: val
.poll_interval_ms
.filter(|interval| !interval.is_nan() && interval.is_finite() && *interval > 0.0)
.map(|interval| Duration::from_secs_f64(interval / 1000.0)),
}
}
}

impl From<NapiProjectOptions> for ProjectOptions {
fn from(val: NapiProjectOptions) -> Self {
ProjectOptions {
root_path: val.root_path.into(),
project_path: val.project_path.into(),
watch: val.watch,
watch: val.watch.into(),
next_config: val.next_config.into(),
js_config: val.js_config.into(),
env: val
Expand All @@ -211,7 +233,7 @@
PartialProjectOptions {
root_path: val.root_path.map(From::from),
project_path: val.project_path.map(From::from),
watch: val.watch,
watch: val.watch.map(From::from),
next_config: val.next_config.map(From::from),
js_config: val.js_config.map(From::from),
env: val.env.map(|env| {
Expand Down Expand Up @@ -909,7 +931,7 @@
}
}

/// Subscribes to lifecycle events of the compilation.

Check warning on line 934 in crates/napi/src/next_api/project.rs

View workflow job for this annotation

GitHub Actions / rustdoc check / build

public documentation for `project_update_info_subscribe` links to private item `UpdateMessage::Start`

Check warning on line 934 in crates/napi/src/next_api/project.rs

View workflow job for this annotation

GitHub Actions / rustdoc check / build

public documentation for `project_update_info_subscribe` links to private item `UpdateMessage::End`

Check warning on line 934 in crates/napi/src/next_api/project.rs

View workflow job for this annotation

GitHub Actions / rustdoc check / build

public documentation for `project_update_info_subscribe` links to private item `UpdateMessage::End`

Check warning on line 934 in crates/napi/src/next_api/project.rs

View workflow job for this annotation

GitHub Actions / rustdoc check / build

public documentation for `project_update_info_subscribe` links to private item `UpdateMessage::Start`
/// Emits an [UpdateMessage::Start] event when any computation starts.
/// Emits an [UpdateMessage::End] event when there was no computation for the
/// specified time (`aggregation_ms`). The [UpdateMessage::End] event contains
Expand Down
42 changes: 31 additions & 11 deletions crates/next-api/src/project.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::path::MAIN_SEPARATOR;
use std::{path::MAIN_SEPARATOR, time::Duration};

use anyhow::{bail, Context, Result};
use indexmap::{indexmap, map::Entry, IndexMap};
Expand Down Expand Up @@ -78,6 +78,19 @@ pub struct DraftModeOptions {
pub preview_mode_signing_key: RcStr,
}

#[derive(
Debug, Default, Serialize, Deserialize, Copy, Clone, TaskInput, PartialEq, Eq, Hash, TraceRawVcs,
)]
#[serde(rename_all = "camelCase")]
pub struct WatchOptions {
/// Whether to watch the filesystem for file changes.
pub enable: bool,

/// Enable polling at a certain interval if the native file watching doesn't work (e.g.
/// docker).
pub poll_interval: Option<Duration>,
}

#[derive(Debug, Serialize, Deserialize, Clone, TaskInput, PartialEq, Eq, Hash, TraceRawVcs)]
#[serde(rename_all = "camelCase")]
pub struct ProjectOptions {
Expand All @@ -101,8 +114,8 @@ pub struct ProjectOptions {
/// time.
pub define_env: DefineEnv,

/// Whether to watch the filesystem for file changes.
pub watch: bool,
/// Filesystem watcher options.
pub watch: WatchOptions,

/// The mode in which Next.js is running.
pub dev: bool,
Expand Down Expand Up @@ -143,8 +156,8 @@ pub struct PartialProjectOptions {
/// time.
pub define_env: Option<DefineEnv>,

/// Whether to watch the filesystem for file changes.
pub watch: Option<bool>,
/// Filesystem watcher options.
pub watch: Option<WatchOptions>,

/// The mode in which Next.js is running.
pub dev: Option<bool>,
Expand Down Expand Up @@ -203,13 +216,16 @@ impl ProjectContainer {
impl ProjectContainer {
#[tracing::instrument(level = "info", name = "initialize project", skip_all)]
pub async fn initialize(self: Vc<Self>, options: ProjectOptions) -> Result<()> {
let poll_interval = options.watch.poll_interval;

self.await?.options_state.set(Some(options));

let project = self.project();
project
.project_fs()
.strongly_consistent()
.await?
.start_watching_with_invalidation_reason()?;
.start_watching_with_invalidation_reason(poll_interval)?;
project
.output_fs()
.strongly_consistent()
Expand Down Expand Up @@ -282,13 +298,15 @@ impl ProjectContainer {
let prev_project_fs = project.project_fs().strongly_consistent().await?;
let prev_output_fs = project.output_fs().strongly_consistent().await?;

let poll_interval = new_options.watch.poll_interval;

this.options_state.set(Some(new_options));
let project_fs = project.project_fs().strongly_consistent().await?;
let output_fs = project.output_fs().strongly_consistent().await?;

if !ReadRef::ptr_eq(&prev_project_fs, &project_fs) {
// TODO stop watching: prev_project_fs.stop_watching()?;
project_fs.start_watching_with_invalidation_reason()?;
project_fs.start_watching_with_invalidation_reason(poll_interval)?;
}
if !ReadRef::ptr_eq(&prev_output_fs, &output_fs) {
prev_output_fs.invalidate_with_reason();
Expand Down Expand Up @@ -407,8 +425,8 @@ pub struct Project {
/// A path inside the root_path which contains the app/pages directories.
pub project_path: RcStr,

/// Whether to watch the filesystem for file changes.
watch: bool,
/// Filesystem watcher options.
watch: WatchOptions,

/// Next config.
next_config: Vc<NextConfig>,
Expand Down Expand Up @@ -521,8 +539,10 @@ impl Project {
self.root_path.clone(),
vec![],
);
if self.watch {
disk_fs.await?.start_watching_with_invalidation_reason()?;
if self.watch.enable {
disk_fs
.await?
.start_watching_with_invalidation_reason(self.watch.poll_interval)?;
}
Ok(disk_fs)
}
Expand Down
4 changes: 2 additions & 2 deletions crates/next-build-test/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@ pub async fn main_inner(

if matches!(strat, Strategy::Development { .. }) {
options.dev = true;
options.watch = true;
options.watch.enable = true;
} else {
options.dev = false;
options.watch = false;
options.watch.enable = false;
}

let project = tt
Expand Down
2 changes: 1 addition & 1 deletion crates/next-build-test/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ fn main() {
},
project_path: canonical_path.to_string_lossy().into(),
root_path: "/".into(),
watch: false,
watch: Default::default(),
browserslist_query: "last 1 Chrome versions, last 1 Firefox versions, last 1 \
Safari versions, last 1 Edge versions"
.into(),
Expand Down
4 changes: 3 additions & 1 deletion packages/next/src/build/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1370,7 +1370,9 @@ export default async function build(
dir,
nextConfig: config,
jsConfig: await getTurbopackJsConfig(dir, config),
watch: false,
watch: {
enable: false,
},
dev,
env: process.env as Record<string, string>,
defineEnv: createDefineEnv({
Expand Down
17 changes: 13 additions & 4 deletions packages/next/src/build/swc/generated-native.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,15 @@ export interface NapiDraftModeOptions {
previewModeEncryptionKey: string
previewModeSigningKey: string
}
export interface NapiWatchOptions {
/** Whether to watch the filesystem for file changes. */
enable: boolean
/**
* Enable polling at a certain interval if the native file watching doesn't work (e.g.
* docker).
*/
pollIntervalMs?: number
}
export interface NapiProjectOptions {
/**
* A root path from which all files must be nested under. Trying to access
Expand All @@ -95,8 +104,8 @@ export interface NapiProjectOptions {
* deserializing next.config, so passing it as separate option.
*/
distDir?: string
/** Whether to watch he filesystem for file changes. */
watch: boolean
/** Filesystem watcher options. */
watch: NapiWatchOptions
/** The contents of next.config.js, serialized to JSON. */
nextConfig: string
/** The contents of ts/config read by load-jsconfig, serialized to JSON. */
Expand Down Expand Up @@ -133,8 +142,8 @@ export interface NapiPartialProjectOptions {
* deserializing next.config, so passing it as separate option.
*/
distDir?: string | undefined | null
/** Whether to watch he filesystem for file changes. */
watch?: boolean
/** Filesystem watcher options. */
watch?: NapiWatchOptions
/** The contents of next.config.js, serialized to JSON. */
nextConfig?: string
/** The contents of ts/config read by load-jsconfig, serialized to JSON. */
Expand Down
5 changes: 4 additions & 1 deletion packages/next/src/build/swc/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,10 @@ export interface ProjectOptions {
/**
* Whether to watch the filesystem for file changes.
*/
watch: boolean
watch: {
enable: boolean
pollIntervalMs?: number
}

/**
* The mode in which Next.js is running.
Expand Down
7 changes: 5 additions & 2 deletions packages/next/src/build/webpack-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ const nodePathList = (process.env.NODE_PATH || '')
.split(process.platform === 'win32' ? ';' : ':')
.filter((p) => !!p)

const watchOptions = Object.freeze({
const baseWatchOptions: webpack.Configuration['watchOptions'] = Object.freeze({
aggregateTimeout: 5,
ignored:
// Matches **/node_modules/**, **/.git/** and **/.next/**
Expand Down Expand Up @@ -1105,7 +1105,10 @@ export default async function getBaseWebpackConfig(
...entrypoints,
}
},
watchOptions,
watchOptions: Object.freeze({
...baseWatchOptions,
poll: config.watchOptions?.pollIntervalMs,
}),
output: {
// we must set publicPath to an empty value to override the default of
// auto which doesn't work in IE11
Expand Down
5 changes: 5 additions & 0 deletions packages/next/src/server/config-schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -627,5 +627,10 @@ export const configSchema: zod.ZodType<NextConfig> = z.lazy(() =>
useFileSystemPublicRoutes: z.boolean().optional(),
// The webpack config type is unknown, use z.any() here
webpack: z.any().nullable().optional(),
watchOptions: z
.strictObject({
pollIntervalMs: z.number().positive().finite().optional(),
})
.optional(),
})
)
4 changes: 4 additions & 0 deletions packages/next/src/server/config-shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -944,6 +944,10 @@ export interface NextConfig extends Record<string, any> {
* were not detected on a per-page basis.
*/
outputFileTracingIncludes?: Record<string, string[]>

watchOptions?: {
pollIntervalMs?: number
}
}

export const defaultConfig: NextConfig = {
Expand Down
5 changes: 4 additions & 1 deletion packages/next/src/server/dev/hot-reloader-turbopack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,10 @@ export async function createHotReloaderTurbopack(
dir,
nextConfig: opts.nextConfig,
jsConfig: await getTurbopackJsConfig(dir, nextConfig),
watch: dev,
watch: {
enable: dev,
pollIntervalMs: nextConfig.watchOptions?.pollIntervalMs,
},
dev,
env: process.env as Record<string, string>,
defineEnv: createDefineEnv({
Expand Down
4 changes: 3 additions & 1 deletion test/development/basic/next-rs-api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,9 @@ describe('next.rs api', () => {
rootPath: process.env.NEXT_SKIP_ISOLATE
? path.resolve(__dirname, '../../..')
: next.testDir,
watch: true,
watch: {
enable: true,
},
dev: true,
defineEnv: createDefineEnv({
isTurbopack: true,
Expand Down
2 changes: 1 addition & 1 deletion turbopack/crates/node-file-trace/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ impl Args {
async fn create_fs(name: &str, root: &str, watch: bool) -> Result<Vc<Box<dyn FileSystem>>> {
let fs = DiskFileSystem::new(name.into(), root.into(), vec![]);
if watch {
fs.await?.start_watching()?;
fs.await?.start_watching(None)?;
} else {
fs.await?.invalidate_with_reason();
}
Expand Down
2 changes: 1 addition & 1 deletion turbopack/crates/turbo-tasks-fs/examples/hash_directory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ async fn main() -> Result<()> {
Box::pin(async {
let root = current_dir().unwrap().to_str().unwrap().into();
let disk_fs = DiskFileSystem::new("project".into(), root, vec![]);
disk_fs.await?.start_watching()?;
disk_fs.await?.start_watching(None)?;

// Smart Pointer cast
let fs: Vc<Box<dyn FileSystem>> = Vc::upcast(disk_fs);
Expand Down
2 changes: 1 addition & 1 deletion turbopack/crates/turbo-tasks-fs/examples/hash_glob.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ async fn main() -> Result<()> {
Box::pin(async {
let root = current_dir().unwrap().to_str().unwrap().into();
let disk_fs = DiskFileSystem::new("project".into(), root, vec![]);
disk_fs.await?.start_watching()?;
disk_fs.await?.start_watching(None)?;

// Smart Pointer cast
let fs: Vc<Box<dyn FileSystem>> = Vc::upcast(disk_fs);
Expand Down
2 changes: 1 addition & 1 deletion turbopack/crates/turbo-tasks-fs/src/embed/dir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pub async fn directory_from_relative_path(
path: RcStr,
) -> Result<Vc<Box<dyn FileSystem>>> {
let disk_fs = DiskFileSystem::new(name, path, vec![]);
disk_fs.await?.start_watching()?;
disk_fs.await?.start_watching(None)?;

Ok(Vc::upcast(disk_fs))
}
Expand Down
2 changes: 1 addition & 1 deletion turbopack/crates/turbo-tasks-fs/src/embed/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ pub async fn content_from_relative_path(
root_path.to_string_lossy().into(),
vec![],
);
disk_fs.await?.start_watching()?;
disk_fs.await?.start_watching(None)?;

let fs_path = disk_fs.root().join(path.into());
Ok(fs_path.read())
Expand Down
Loading
Loading