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

⚡️ provide exclude patterns to file watcher args #1607

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
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
3 changes: 2 additions & 1 deletion packages/core/src/common/externals/NodeJsExternals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,10 @@ export const NodeJsExternals: Externals = {
unlink(location) {
return fsp.unlink(toFsPathLike(location))
},
watch(locations, { usePolling = false } = {}) {
watch(locations, { ignored, usePolling } = {}) {
return new ChokidarWatcherWrapper(
chokidar.watch(locations.map(toPath), {
ignored,
usePolling,
disableGlobbing: true,
}),
Expand Down
6 changes: 5 additions & 1 deletion packages/core/src/common/externals/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { WatchOptions } from 'chokidar'
import type { Uri } from '../util.js'
import type { ExternalDownloader } from './downloader.js'

Expand Down Expand Up @@ -72,7 +73,10 @@ export interface ExternalFileSystem {
showFile(path: FsLocation): Promise<void>
stat(location: FsLocation): Promise<{ isDirectory(): boolean; isFile(): boolean }>
unlink(location: FsLocation): Promise<void>
watch(locations: FsLocation[], options: { usePolling?: boolean }): FsWatcher
watch(
locations: FsLocation[],
options: WatchOptions,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This shouldn't directly use chokidar's WatchOptions, this is an interface that needs to be shared between the nodejs and browser externals

): FsWatcher
/**
* @param options `mode` - File mode bit mask (e.g. `0o775`).
*/
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/service/Config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@ export const VanillaConfig: Config = {
env: {
dataSource: 'GitHub',
dependencies: ['@vanilla-datapack', '@vanilla-mcdoc'],
exclude: ['@gitignore', '.vscode/', '.github/'],
exclude: ['@gitignore', '.*'],
customResources: {},
feature: {
codeActions: true,
Expand Down
48 changes: 46 additions & 2 deletions packages/core/src/service/Project.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import type { Ignore } from 'ignore'
import ignore from 'ignore'
import { isAbsolute, resolve as resolvePath } from 'path'
import url from 'url'
import type { TextDocumentContentChangeEvent } from 'vscode-languageserver-textdocument'
import { TextDocument } from 'vscode-languageserver-textdocument'
import type { ExternalEventEmitter, Externals, FsWatcher, IntervalId } from '../common/index.js'
Expand Down Expand Up @@ -185,6 +187,7 @@ export class Project implements ExternalEventEmitter {
config!: Config
ignore: Ignore = ignore()
readonly downloader: Downloader
readonly #excludePaths: string[] = []
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is weirdly named, it should be excludePatterns

readonly externals: Externals
readonly fs: FileService
readonly isDebugging: boolean
Expand Down Expand Up @@ -395,16 +398,30 @@ export class Project implements ExternalEventEmitter {
const loadConfig = async () => {
this.config = await this.#configService.load()
this.ignore = ignore()
await parseExcludePaths()
for (const pattern of this.#excludePaths) {
this.ignore.add(pattern)
}
}

const parseExcludePaths = async () => {
const paths = []
for (const pattern of this.config.env.exclude) {
if (pattern === '@gitignore') {
const gitignore = await this.readGitignore()
if (gitignore) {
this.ignore.add(gitignore)
const gitignoreLines = gitignore.split(/\r?\n/)
const gitignorePaths = gitignoreLines
.filter((line) => line !== '' && !line.startsWith('#'))
paths.push(...gitignorePaths)
}
} else {
this.ignore.add(pattern)
paths.push(pattern)
}
}
// Chokidar never matches paths with a trailing (back)slash, so fix any paths
// that may be specified as such
this.#excludePaths.push(...paths.map((path) => path.replace(/(\\|\/)$/, '')))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this is a chokidar specific requirement, this should be moved to the NodeJsExternals watch handler.

}
const callIntializers = async () => {
const initCtx: ProjectInitializerContext = {
Expand Down Expand Up @@ -514,9 +531,36 @@ export class Project implements ExternalEventEmitter {
resolve()
return
}

const ignored = []
const absoluteExcludePaths = this.#excludePaths
.filter(isAbsolute)
.map((path) => resolvePath(path))
const matchesAbsolutePath = (path: string): boolean => {
return absoluteExcludePaths.some((exclude) => {
if (process.platform === 'win32') {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will break in the browser

const normalizeWinPath = (p: string) => resolvePath(p).toLowerCase()
return normalizeWinPath(path) === normalizeWinPath(exclude)
} else {
return exclude === path
}
})
}
ignored.push(matchesAbsolutePath)

const relativeExcludePaths = this.#excludePaths.filter((path) => !isAbsolute(path))
// Need to list the full file paths for chokidar to match
for (const rootUri of this.projectRoots) {
const absolutePaths = relativeExcludePaths.map((path) =>
url.fileURLToPath(`${rootUri}${path}`)
)
ignored.push(...absolutePaths)
}

this.#watchedFiles.clear()
this.#watcherReady = false
this.#watcher = this.externals.fs.watch(this.projectRoots, {
ignored,
usePolling: this.config.env.useFilePolling,
}).once('ready', () => {
this.#watcherReady = true
Expand Down
Loading