From 36cc19c1b40566a8431df5ce514765a70481d36e Mon Sep 17 00:00:00 2001 From: Zed Spencer-Milnes Date: Sat, 14 Dec 2024 21:47:42 +0000 Subject: [PATCH] fix(#10769) Add workaround for microsoft/vscode-remote-release#7029 by using IPv4 loopback when in Remote Container environment --- packages/docusaurus/src/commands/serve.ts | 3 +++ .../docusaurus/src/commands/start/start.ts | 3 +++ .../src/server/remoteContainersHook.ts | 27 +++++++++++++++++++ 3 files changed, 33 insertions(+) create mode 100644 packages/docusaurus/src/server/remoteContainersHook.ts diff --git a/packages/docusaurus/src/commands/serve.ts b/packages/docusaurus/src/commands/serve.ts index 85b02dd2af0f..05d7a12f9164 100644 --- a/packages/docusaurus/src/commands/serve.ts +++ b/packages/docusaurus/src/commands/serve.ts @@ -16,6 +16,7 @@ import {applyTrailingSlash} from '@docusaurus/utils-common'; import {loadSiteConfig} from '../server/config'; import {build} from './build/build'; import {getHostPort, type HostPortOptions} from '../server/getHostPort'; +import {forceV4OnRemoteContainers} from '../server/remoteContainersHook'; import type {LoadContextParams} from '../server/site'; function redirect(res: http.ServerResponse, location: string) { @@ -41,6 +42,8 @@ export async function serve( const buildDir = cliOptions.dir ?? DEFAULT_BUILD_DIR_NAME; const outDir = path.resolve(siteDir, buildDir); + forceV4OnRemoteContainers(cliOptions); + if (cliOptions.build) { await build(siteDir, { config: cliOptions.config, diff --git a/packages/docusaurus/src/commands/start/start.ts b/packages/docusaurus/src/commands/start/start.ts index e0e5595e52c5..7e27f011627b 100644 --- a/packages/docusaurus/src/commands/start/start.ts +++ b/packages/docusaurus/src/commands/start/start.ts @@ -10,6 +10,7 @@ import openBrowser from 'react-dev-utils/openBrowser'; import {setupSiteFileWatchers} from './watcher'; import {createWebpackDevServer} from './webpack'; import {createReloadableSite} from './utils'; +import {forceV4OnRemoteContainers} from '../../server/remoteContainersHook'; import type {LoadContextParams} from '../../server/site'; import type {HostPortOptions} from '../../server/getHostPort'; @@ -31,6 +32,8 @@ export async function start( // See https://github.com/facebook/docusaurus/issues/4542 process.env.DOCUSAURUS_CURRENT_LOCALE = cliOptions.locale; + forceV4OnRemoteContainers(cliOptions); + const reloadableSite = await createReloadableSite({siteDirParam, cliOptions}); setupSiteFileWatchers( diff --git a/packages/docusaurus/src/server/remoteContainersHook.ts b/packages/docusaurus/src/server/remoteContainersHook.ts new file mode 100644 index 000000000000..fff1e3739642 --- /dev/null +++ b/packages/docusaurus/src/server/remoteContainersHook.ts @@ -0,0 +1,27 @@ +/** + * Copyright (c) Facebook, Inc. and its affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +import logger from '@docusaurus/logger'; + +export function forceV4OnRemoteContainers( + userCLIConfig: OptionallyProvidedHost, +): void { + if (process.env.REMOTE_CONTAINERS === 'true') { + if (!userCLIConfig.host) { + userCLIConfig.host = '127.0.0.1'; + logger.info( + 'Will default to binding to IPv4 local address to better support VSCode Remote port forwarding', + ); + } else { + logger.warn('VSCode Remote may not support IPv6'); + } + } +} + +export type OptionallyProvidedHost = { + host?: string; +};