Skip to content

Change Request: includeIgnoreFile should respect the path to the directory containing the .gitignore #329

@keithyipkw

Description

@keithyipkw

Which packages would you like to change?

  • @eslint/compat
  • @eslint/config-array
  • @eslint/config-helpers
  • @eslint/core
  • @eslint/mcp
  • @eslint/migrate-config
  • @eslint/object-schema
  • @eslint/plugin-kit

What problem do you want to solve?

The current includeIgnoreFile only supports the .gitignore in the root directory. .gitignore in subfolders are common monorepo and even single project repo.

Quote from https://git-scm.com/docs/gitignore

These patterns match relative to the location of the .gitignore file

The following .gitignore in folderA should

# ignore `folderA/**/abc`, not `**/abc`
abc

# ignore `folderA/build`, not `build`
/build

What do you think is the correct solution?

The following code is a rough fix. ignoreFilePath is relative to the folder containing the config file (not sure if this is the convention) or an absolute path. I do not know how to get the path to the folder containing the config file, so I pass it in from the outside. The code will crash if users do not set configDir after updating, or likely to crash if the name becomes configDir.

export function includeIgnoreFile(ignoreFilePath, configDir, name) {
	if (!path.isAbsolute(ignoreFilePath)) {
		ignoreFilePath = path.join(configDir, ignoreFilePath)
	}

	const ignoreFile = fs.readFileSync(ignoreFilePath, "utf8");
	const lines = ignoreFile.split(/\r?\n/u);
	
	let dir = path.relative(configDir, path.dirname(ignoreFilePath));
	if (process.platform == 'win32') {
		dir = dir.replace(/\\/g, '/');
	}

	return {
		name: name || "Imported .gitignore patterns",
		ignores: lines
			.map(line => line.trim())
			.filter(line => line && !line.startsWith("#"))
			.map(line => {
				const isNegated = line.startsWith("!");
				const negatedPrefix = isNegated ? "!" : "";
				const pattern = isNegated ? line.slice(1) : line;
				return negatedPrefix + path.posix.join(dir, convertIgnorePatternToMinimatch(pattern));
			}),
	};
}

// usage
includeIgnoreFile('folder/.gitignore', import.meta.dirname);

Participation

  • I am willing to submit a pull request for this change.

Additional comments

No response

Metadata

Metadata

Labels

acceptedenhancementNew feature or requestrepro:yesIssues with a reproducible example

Type

No type

Projects

Status

Implementing

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions