{"record":{"id":"e21564adb2d2fc47","repo":"nodejs/node","slug":"index-value-d-not-in-expected-range-0-d","errorCode":null,"errorMessage":"index value (%d) not in expected range [0, %d)","messagePattern":"index value \\((.+?)\\) not in expected range \\[0, (.+?)\\)","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"tools/gyp/pylib/gyp/MSVSSettings.py","lineNumber":209,"sourceCode":"    def __init__(self, label_list, new=None):\n        _Type.__init__(self)\n        self._label_list = label_list\n        self._msbuild_values = {value for value in label_list if value is not None}\n        if new is not None:\n            self._msbuild_values.update(new)\n\n    def ValidateMSVS(self, value):\n        # Try to convert.  It will raise an exception if not valid.\n        self.ConvertToMSBuild(value)\n\n    def ValidateMSBuild(self, value):\n        if value not in self._msbuild_values:\n            raise ValueError(\"unrecognized enumerated value %s\" % value)\n\n    def ConvertToMSBuild(self, value):\n        index = int(value)\n        if index < 0 or index >= len(self._label_list):\n            raise ValueError(\n                \"index value (%d) not in expected range [0, %d)\"\n                % (index, len(self._label_list))\n            )\n        label = self._label_list[index]\n        if label is None:\n            raise ValueError(\"converted value for %s not specified.\" % value)\n        return label\n\n\n# Instantiate the various generic types.\n_boolean = _Boolean()\n_integer = _Integer()\n# For now, we don't do any special validation on these types:\n_string = _String()\n_file_name = _String()\n_folder_name = _String()\n_file_list = _StringList()\n_folder_list = _StringList()","sourceCodeStart":191,"sourceCodeEnd":227,"githubUrl":"https://github.com/nodejs/node/blob/1b2de5e052fc0fb95fd7fb6846dcec4ade598e9e/tools/gyp/pylib/gyp/MSVSSettings.py#L191-L227","documentation":"_Enumeration.ConvertToMSBuild raises ValueError('index value (%d) not in expected range [0, %d)') when an MSVS enumeration is supplied as an integer index that is negative or >= the length of the enumeration's _label_list. The MSVS side stores enumerations as integer indices; ConvertToMSBuild maps each index to a label. An out-of-range index means the source gyp value does not correspond to any known option.","triggerScenarios":"Setting an MSVS enumeration to an integer (or numeric string) outside the supported range, e.g. 'Optimization': '5' when only indices 0..4 are defined; computing an index dynamically and going off the end.","commonSituations":"Hardcoding a numeric value copied from an old VS version whose enumeration had more entries; off-by-one when deriving the index from a list lookup.","solutions":["Open MSVSSettings.py, find the setting's _Enumeration([...]) definition, and use an index within [0, len(labels)).","Prefer the MSBuild label string form to avoid index arithmetic entirely.","Bound the computed index: index = max(0, min(index, len(labels) - 1))."],"exampleFix":"# before\n# _Enumeration(['Disabled','MinSpace','MinSize','Full','MaxSpeed']) -> indices 0..4\n'Optimization': '5',\n\n# after\n'Optimization': '4',   # 'MaxSpeed'","handlingStrategy":"validation","validationCode":"def clamp_index(index, label_list):\n    i = int(index)\n    if i < 0 or i >= len(label_list):\n        raise ValueError('index %d out of range [0,%d)' % (i, len(label_list)))\n    return i","typeGuard":"def is_in_range_index(value, label_list) -> bool:\n    try:\n        return 0 <= int(value) < len(label_list)\n    except (TypeError, ValueError):\n        return False","tryCatchPattern":"try:\n    enum_type.ConvertToMSBuild(my_value)\nexcept ValueError:\n    my_value = str(min(max(int(my_value), 0), len(enum_type._label_list) - 1))\n    enum_type.ConvertToMSBuild(my_value)","preventionTips":["Look up the enumeration's label list length before using a numeric index.","Prefer MSBuild label strings to avoid index arithmetic.","Bound computed indices with min/max against the label list."],"tags":["gyp","msvs","enumeration","validation","range"],"backgroundTag":null,"analyzedSha":"1b2de5e052fc0fb95fd7fb6846dcec4ade598e9e","analyzedAt":"2026-08-13T00:53:24.642Z","schemaVersion":2},"datasetVersion":"2026-08-13T04:17:16.726Z"}