From 3cd22f259646c8df277dccda3e96c23d90b2fc06 Mon Sep 17 00:00:00 2001
From: Varun Gandhi <varun.gandhi@sourcegraph.com>
Date: Fri, 18 Aug 2023 13:02:57 +0800
Subject: [PATCH 1/5] HACKS: Print debugggggg

---
 packages/pyright-scip/src/ScipSymbol.ts                 | 4 ++++
 packages/pyright-scip/src/lsif-typescript/Descriptor.ts | 7 +++++--
 packages/pyright-scip/src/lsif-typescript/LsifSymbol.ts | 6 +++++-
 packages/pyright-scip/src/symbols.ts                    | 7 +++++++
 packages/pyright-scip/src/virtualenv/PythonPackage.ts   | 6 +++++-
 5 files changed, 26 insertions(+), 4 deletions(-)

diff --git a/packages/pyright-scip/src/ScipSymbol.ts b/packages/pyright-scip/src/ScipSymbol.ts
index 2d005485e..19ed382e0 100644
--- a/packages/pyright-scip/src/ScipSymbol.ts
+++ b/packages/pyright-scip/src/ScipSymbol.ts
@@ -10,6 +10,10 @@ export class ScipSymbol extends TypescriptScipSymbol {
         name = name.replace(/\./, '/');
         name = name.trim();
 
+        if (name === '.' || name === '/') {
+            throw new Error('Package name cannot be "." or "/"');
+        }
+
         // @ts-ignore
         return new TypescriptScipSymbol(`scip-python python ${name} ${version} `);
     }
diff --git a/packages/pyright-scip/src/lsif-typescript/Descriptor.ts b/packages/pyright-scip/src/lsif-typescript/Descriptor.ts
index cdf036375..4a9027877 100644
--- a/packages/pyright-scip/src/lsif-typescript/Descriptor.ts
+++ b/packages/pyright-scip/src/lsif-typescript/Descriptor.ts
@@ -6,7 +6,10 @@ type Suffix = scip.Descriptor.Suffix;
 const Suffix = scip.Descriptor.Suffix;
 
 export function packageDescriptor(name: string): Descriptor {
-    return new Descriptor({ name, suffix: Suffix.Package });
+    if (name === '') {
+        throw new Error('Package name cannot be empty');
+    }
+    return new Descriptor({ name, suffix: Suffix.Namespace });
 }
 
 export function typeDescriptor(name: string): Descriptor {
@@ -35,7 +38,7 @@ export function typeParameterDescriptor(name: string): Descriptor {
 
 export function descriptorString(desc: Descriptor): string {
     switch (desc.suffix) {
-        case Suffix.Package:
+        case Suffix.Namespace:
             return escapedName(desc) + '/';
         case Suffix.Type:
             return escapedName(desc) + '#';
diff --git a/packages/pyright-scip/src/lsif-typescript/LsifSymbol.ts b/packages/pyright-scip/src/lsif-typescript/LsifSymbol.ts
index 4ebcf05f1..1808d3b83 100644
--- a/packages/pyright-scip/src/lsif-typescript/LsifSymbol.ts
+++ b/packages/pyright-scip/src/lsif-typescript/LsifSymbol.ts
@@ -25,6 +25,10 @@ export class LsifSymbol {
     }
 
     public static global(owner: LsifSymbol, descriptor: scip.Descriptor): LsifSymbol {
-        return new LsifSymbol(owner.value + descriptorString(descriptor));
+        const s = descriptorString(descriptor);
+        if (s.indexOf('SuchNestedMuchWow') !== -1) {
+            throw new Error('Got problematic descriptor with owner: ' + owner.value);
+        }
+        return new LsifSymbol(owner.value + s);
     }
 }
diff --git a/packages/pyright-scip/src/symbols.ts b/packages/pyright-scip/src/symbols.ts
index a3b3d5b15..d38239346 100644
--- a/packages/pyright-scip/src/symbols.ts
+++ b/packages/pyright-scip/src/symbols.ts
@@ -26,6 +26,13 @@ export function makeModule(pythonPackage: PythonPackage, moduleName: string): Sc
     if (ns) {
         moduleName = ns + '.' + moduleName;
     }
+    if (moduleName === '') {
+        throw new Error(
+            `Module name is empty namespace = ${ns}, pkgname = ${pythonPackage.name}, map = ${JSON.stringify(
+                Object.fromEntries(namespaces)
+            )}`
+        );
+    }
 
     return ScipSymbol.global(makePackage(pythonPackage), packageDescriptor(moduleName));
 }
diff --git a/packages/pyright-scip/src/virtualenv/PythonPackage.ts b/packages/pyright-scip/src/virtualenv/PythonPackage.ts
index ec11ea445..203046576 100644
--- a/packages/pyright-scip/src/virtualenv/PythonPackage.ts
+++ b/packages/pyright-scip/src/virtualenv/PythonPackage.ts
@@ -3,7 +3,11 @@ import * as path from 'path';
 const validExtensions = new Set(['.py', '.pyi']);
 
 export default class PythonPackage {
-    constructor(public name: string, public version: string, public files: string[]) {}
+    constructor(public name: string, public version: string, public files: string[]) {
+        if (name === '.' || name === '/') {
+            throw new Error('Package name cannot be "." or "/"');
+        }
+    }
 
     static fromPipShow(output: string): PythonPackage {
         let name = '';

From e61f73aff4977a01cb63e39eec7380ce35774b58 Mon Sep 17 00:00:00 2001
From: Varun Gandhi <varun.gandhi@sourcegraph.com>
Date: Fri, 18 Aug 2023 15:39:53 +0800
Subject: [PATCH 2/5] Print namespaces earlier

---
 packages/pyright-scip/src/symbols.ts | 13 ++++++++++++-
 1 file changed, 12 insertions(+), 1 deletion(-)

diff --git a/packages/pyright-scip/src/symbols.ts b/packages/pyright-scip/src/symbols.ts
index d38239346..04c1f8557 100644
--- a/packages/pyright-scip/src/symbols.ts
+++ b/packages/pyright-scip/src/symbols.ts
@@ -1,6 +1,7 @@
 import { ParseNode } from 'pyright-internal/parser/parseNodes';
 import { Counter } from './lsif-typescript/Counter';
 import {
+    descriptorString,
     metaDescriptor,
     methodDescriptor,
     packageDescriptor,
@@ -74,7 +75,17 @@ export function makeMethod(parent: ScipSymbol, name: string): ScipSymbol {
 }
 
 export function makeType(parent: ScipSymbol, name: string): ScipSymbol {
-    return ScipSymbol.global(parent, typeDescriptor(name));
+    const desc = typeDescriptor(name);
+    const descString = descriptorString(desc);
+    if (descString.indexOf('SuchNestedMuchWow') !== -1) {
+        throw new Error(
+            'Got problematic descriptor with owner: ' +
+                parent.value +
+                ', namespaces: ' +
+                JSON.stringify(Object.fromEntries(namespaces))
+        );
+    }
+    return ScipSymbol.global(parent, desc);
 }
 
 export function makeTerm(parent: ScipSymbol, name: string): ScipSymbol {

From fe761adae2856e4df38f0b13978c35f8ab0f2f37 Mon Sep 17 00:00:00 2001
From: Varun Gandhi <varun.gandhi@sourcegraph.com>
Date: Tue, 22 Aug 2023 10:33:02 +0800
Subject: [PATCH 3/5] Debug parent symbol

---
 .../pyright-scip/src/lsif-typescript/Descriptor.ts |  6 +++---
 packages/pyright-scip/src/symbols.ts               | 14 +++++++-------
 packages/pyright-scip/src/treeVisitor.ts           |  7 ++++++-
 3 files changed, 16 insertions(+), 11 deletions(-)

diff --git a/packages/pyright-scip/src/lsif-typescript/Descriptor.ts b/packages/pyright-scip/src/lsif-typescript/Descriptor.ts
index 4a9027877..1380885c3 100644
--- a/packages/pyright-scip/src/lsif-typescript/Descriptor.ts
+++ b/packages/pyright-scip/src/lsif-typescript/Descriptor.ts
@@ -6,9 +6,9 @@ type Suffix = scip.Descriptor.Suffix;
 const Suffix = scip.Descriptor.Suffix;
 
 export function packageDescriptor(name: string): Descriptor {
-    if (name === '') {
-        throw new Error('Package name cannot be empty');
-    }
+    // if (name === '') {
+    //     throw new Error('Package name cannot be empty');
+    // }
     return new Descriptor({ name, suffix: Suffix.Namespace });
 }
 
diff --git a/packages/pyright-scip/src/symbols.ts b/packages/pyright-scip/src/symbols.ts
index 04c1f8557..f1e91ed62 100644
--- a/packages/pyright-scip/src/symbols.ts
+++ b/packages/pyright-scip/src/symbols.ts
@@ -27,13 +27,13 @@ export function makeModule(pythonPackage: PythonPackage, moduleName: string): Sc
     if (ns) {
         moduleName = ns + '.' + moduleName;
     }
-    if (moduleName === '') {
-        throw new Error(
-            `Module name is empty namespace = ${ns}, pkgname = ${pythonPackage.name}, map = ${JSON.stringify(
-                Object.fromEntries(namespaces)
-            )}`
-        );
-    }
+    // if (moduleName === '') {
+    //     throw new Error(
+    //         `Module name is empty namespace = ${ns}, pkgname = ${pythonPackage.name}, map = ${JSON.stringify(
+    //             Object.fromEntries(namespaces)
+    //         )}`
+    //     );
+    // }
 
     return ScipSymbol.global(makePackage(pythonPackage), packageDescriptor(moduleName));
 }
diff --git a/packages/pyright-scip/src/treeVisitor.ts b/packages/pyright-scip/src/treeVisitor.ts
index 42fcd79e7..064b73bd8 100644
--- a/packages/pyright-scip/src/treeVisitor.ts
+++ b/packages/pyright-scip/src/treeVisitor.ts
@@ -1091,7 +1091,12 @@ export class TreeVisitor extends ParseTreeWalker {
                 return Symbols.makeParameter(this.getScipSymbol(node.parent!), node.name.value);
             }
             case ParseNodeType.Class: {
-                return Symbols.makeType(this.getScipSymbol(node.parent!), (node as ClassNode).name.value);
+                const x = (node as ClassNode).name.value;
+                const parentSym = this.getScipSymbol(node.parent!);
+                if (x.indexOf('SuchNestedMuchWow') !== -1) {
+                    console.log(`parentSym = ${parentSym.value} for SuchNestedMuchWow`);
+                }
+                return Symbols.makeType(parentSym, x);
             }
             case ParseNodeType.Function: {
                 let cls = ParseTreeUtils.getEnclosingClass(node, false);

From d281e5ff6aabf4e280167547499844f481af1c9b Mon Sep 17 00:00:00 2001
From: Varun Gandhi <varun.gandhi@sourcegraph.com>
Date: Tue, 22 Aug 2023 14:08:36 +0800
Subject: [PATCH 4/5] More debugging

---
 packages/pyright-scip/src/treeVisitor.ts | 19 ++++++++++++++++---
 1 file changed, 16 insertions(+), 3 deletions(-)

diff --git a/packages/pyright-scip/src/treeVisitor.ts b/packages/pyright-scip/src/treeVisitor.ts
index 064b73bd8..acec0626f 100644
--- a/packages/pyright-scip/src/treeVisitor.ts
+++ b/packages/pyright-scip/src/treeVisitor.ts
@@ -894,9 +894,16 @@ export class TreeVisitor extends ParseTreeWalker {
         }
     }
 
-    private getScipSymbol(node: ParseNode, opts: ScipSymbolOptions | undefined = undefined): ScipSymbol {
+    private getScipSymbol(
+        node: ParseNode,
+        debug: boolean = false,
+        opts: ScipSymbolOptions | undefined = undefined
+    ): ScipSymbol {
         const existing = this.rawGetLsifSymbol(node);
         if (existing) {
+            if (debug) {
+                console.log('got existing');
+            }
             return existing;
         }
 
@@ -922,6 +929,9 @@ export class TreeVisitor extends ParseTreeWalker {
             }
 
             moduleName = nodeFileInfo.moduleName;
+            if (debug) {
+                console.log(`moduleName = ${moduleName}`);
+            }
             if (moduleName == 'builtins') {
                 return this.emitBuiltinScipSymbol(node);
             } else if (Hardcoded.stdlib_module_names.has(moduleName)) {
@@ -1092,11 +1102,14 @@ export class TreeVisitor extends ParseTreeWalker {
             }
             case ParseNodeType.Class: {
                 const x = (node as ClassNode).name.value;
-                const parentSym = this.getScipSymbol(node.parent!);
+                const parentSym =
+                    x.indexOf('SuchNestedMuchWow') !== -1
+                        ? this.getScipSymbol(node.parent!, true)
+                        : this.getScipSymbol(node.parent!);
                 if (x.indexOf('SuchNestedMuchWow') !== -1) {
                     console.log(`parentSym = ${parentSym.value} for SuchNestedMuchWow`);
                 }
-                return Symbols.makeType(parentSym, x);
+                return Symbols.makeType(parentSym, x); // Varun: bad call here
             }
             case ParseNodeType.Function: {
                 let cls = ParseTreeUtils.getEnclosingClass(node, false);

From f1a3c4544a12a18503460c48d2cc1db00d121dc4 Mon Sep 17 00:00:00 2001
From: Varun Gandhi <varun.gandhi@sourcegraph.com>
Date: Tue, 22 Aug 2023 14:15:34 +0800
Subject: [PATCH 5/5] More debugging

---
 packages/pyright-scip/src/treeVisitor.ts | 1 +
 1 file changed, 1 insertion(+)

diff --git a/packages/pyright-scip/src/treeVisitor.ts b/packages/pyright-scip/src/treeVisitor.ts
index acec0626f..af8a90c6a 100644
--- a/packages/pyright-scip/src/treeVisitor.ts
+++ b/packages/pyright-scip/src/treeVisitor.ts
@@ -931,6 +931,7 @@ export class TreeVisitor extends ParseTreeWalker {
             moduleName = nodeFileInfo.moduleName;
             if (debug) {
                 console.log(`moduleName = ${moduleName}`);
+                console.log(`filePath = ${nodeFileInfo.filePath}`);
             }
             if (moduleName == 'builtins') {
                 return this.emitBuiltinScipSymbol(node);
