From 7a92594135d246639aa1f545781b4add1fb7c777 Mon Sep 17 00:00:00 2001 From: Myzel394 <50424412+Myzel394@users.noreply.github.com> Date: Sat, 15 Feb 2025 01:00:15 +0100 Subject: [PATCH] fix(server): Improve spec field analyzer --- server/doc-values/value-path.go | 14 +++++---- server/handlers/fstab/analyzer/spec.go | 38 ++++++++++++++++++++++++ server/handlers/fstab/analyzer/values.go | 2 ++ server/handlers/fstab/fields/spec.go | 3 +- 4 files changed, 51 insertions(+), 6 deletions(-) create mode 100644 server/handlers/fstab/analyzer/spec.go diff --git a/server/doc-values/value-path.go b/server/doc-values/value-path.go index 1aced7c..2ca9a8c 100644 --- a/server/doc-values/value-path.go +++ b/server/doc-values/value-path.go @@ -48,11 +48,15 @@ func (v PathValue) GetTypeDescription() []string { func (v PathValue) DeprecatedCheckIsValid(value string) []*InvalidValue { if !utils.DoesPathExist(value) { - return []*InvalidValue{{ - Err: PathDoesNotExistError{}, - Start: 0, - End: uint32(len(value)), - }} + if v.RequiredType == PathTypeExistenceOptional { + return nil + } else { + return []*InvalidValue{{ + Err: PathDoesNotExistError{}, + Start: 0, + End: uint32(len(value)), + }} + } } isValid := false diff --git a/server/handlers/fstab/analyzer/spec.go b/server/handlers/fstab/analyzer/spec.go new file mode 100644 index 0000000..4b3b382 --- /dev/null +++ b/server/handlers/fstab/analyzer/spec.go @@ -0,0 +1,38 @@ +package analyzer + +import ( + "config-lsp/common" + "config-lsp/handlers/fstab/ast" + "regexp" + + protocol "github.com/tliron/glsp/protocol_3_16" +) + +var volatileBlockFields = regexp.MustCompile(`^/dev/(sd|nvme|mmcblk|sr|vd|loop|cdrom)[a-zA-Z0-9]*$`) + +func analyzeSpecField( + ctx *analyzerContext, + field *ast.FstabField, +) { + if field == nil { + return + } + + if field.Value.Value == "" { + return + } + + if !volatileBlockFields.MatchString(field.Value.Value) { + return + } + + codeDescription := protocol.CodeDescription{ + HRef: protocol.URI("https://wiki.archlinux.org/title/Persistent_block_device_naming"), + } + ctx.diagnostics = append(ctx.diagnostics, protocol.Diagnostic{ + Range: field.ToLSPRange(), + Message: "Kernel name descriptors for block devices are not persistent and can change each boot, they should not be used in configuration files. Prefer device UUIDs or LABELs instead.", + CodeDescription: &codeDescription, + Severity: &common.SeverityWarning, + }) +} diff --git a/server/handlers/fstab/analyzer/values.go b/server/handlers/fstab/analyzer/values.go index 62d9fac..fcc1fbc 100644 --- a/server/handlers/fstab/analyzer/values.go +++ b/server/handlers/fstab/analyzer/values.go @@ -21,6 +21,8 @@ func analyzeValuesAreValid( checkField(ctx, entry.Fields.MountPoint, fields.MountPointField) checkField(ctx, entry.Fields.FilesystemType, fields.FileSystemTypeField) + analyzeSpecField(ctx, entry.Fields.Spec) + if entry.Fields.Options != nil { mountOptions := entry.FetchMountOptionsField(true) diff --git a/server/handlers/fstab/fields/spec.go b/server/handlers/fstab/fields/spec.go index df43f6d..0602cfa 100644 --- a/server/handlers/fstab/fields/spec.go +++ b/server/handlers/fstab/fields/spec.go @@ -6,7 +6,8 @@ import ( ) var UuidField = docvalues.RegexValue{ - Regex: *regexp.MustCompile(`[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}`), + // Can either be a UUID or UID + Regex: *regexp.MustCompile(`(?i)([a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}|[a-f0-9]{4}-[a-f0-9]{4})`), } var LabelField = docvalues.RegexValue{ Regex: *regexp.MustCompile(`\S+`),