1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104
|
// Copyright 2020 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package diagnostics
import (
"context"
"fmt"
"os/exec"
"testing"
"golang.org/x/tools/gopls/internal/hooks"
"golang.org/x/tools/gopls/internal/lsp"
"golang.org/x/tools/gopls/internal/lsp/fake"
"golang.org/x/tools/gopls/internal/lsp/protocol"
. "golang.org/x/tools/gopls/internal/lsp/regtest"
"golang.org/x/tools/internal/bug"
"golang.org/x/tools/internal/testenv"
)
func TestMain(m *testing.M) {
bug.PanicOnBugs = true
Main(m, hooks.Options)
}
// Use mod.com for all go.mod files due to golang/go#35230.
const exampleProgram = `
-- go.mod --
module mod.com
go 1.12
-- main.go --
package main
import "fmt"
func main() {
fmt.Println("Hello World.")
}`
func TestDiagnosticErrorInEditedFile(t *testing.T) {
// This test is very basic: start with a clean Go program, make an error, and
// get a diagnostic for that error. However, it also demonstrates how to
// combine Expectations to await more complex state in the editor.
Run(t, exampleProgram, func(t *testing.T, env *Env) {
// Deleting the 'n' at the end of Println should generate a single error
// diagnostic.
env.OpenFile("main.go")
env.RegexpReplace("main.go", "Printl(n)", "")
env.Await(
// Once we have gotten diagnostics for the change above, we should
// satisfy the DiagnosticAtRegexp assertion.
OnceMet(
env.DoneWithChange(),
env.DiagnosticAtRegexp("main.go", "Printl"),
),
// Assert that this test has sent no error logs to the client. This is not
// strictly necessary for testing this regression, but is included here
// as an example of using the NoErrorLogs() expectation. Feel free to
// delete.
NoErrorLogs(),
)
})
}
func TestMissingImportDiagsClearOnFirstFile(t *testing.T) {
const onlyMod = `
-- go.mod --
module mod.com
go 1.12
`
Run(t, onlyMod, func(t *testing.T, env *Env) {
env.CreateBuffer("main.go", `package main
func m() {
log.Println()
}
`)
env.Await(
env.DiagnosticAtRegexp("main.go", "log"),
)
env.SaveBuffer("main.go")
env.Await(
EmptyDiagnostics("main.go"),
)
})
}
func TestDiagnosticErrorInNewFile(t *testing.T) {
const brokenFile = `package main
const Foo = "abc
`
Run(t, brokenFile, func(t *testing.T, env *Env) {
env.CreateBuffer("broken.go", brokenFile)
env.Await(env.DiagnosticAtRegexp("broken.go", "\"abc"))
})
}
// badPackage contains a duplicate definition of the 'a' const.
const badPackage = `
-- go.mod --
module mod.com
go 1.12
-- a.go --
package consts
const a = 1
-- b.go --
package consts
const a = 2
`
func TestDiagnosticClearingOnEdit(t *testing.T) {
Run(t, badPackage, func(t *testing.T, env *Env) {
env.OpenFile("b.go")
env.Await(env.DiagnosticAtRegexp("a.go", "a = 1"), env.DiagnosticAtRegexp("b.go", "a = 2"))
// Fix the error by editing the const name in b.go to `b`.
env.RegexpReplace("b.go", "(a) = 2", "b")
env.Await(
EmptyDiagnostics("a.go"),
EmptyDiagnostics("b.go"),
)
})
}
func TestDiagnosticClearingOnDelete_Issue37049(t *testing.T) {
Run(t, badPackage, func(t *testing.T, env *Env) {
env.OpenFile("a.go")
env.Await(env.DiagnosticAtRegexp("a.go", "a = 1"), env.DiagnosticAtRegexp("b.go", "a = 2"))
env.RemoveWorkspaceFile("b.go")
env.Await(EmptyDiagnostics("a.go"), EmptyDiagnostics("b.go"))
})
}
func TestDiagnosticClearingOnClose(t *testing.T) {
Run(t, badPackage, func(t *testing.T, env *Env) {
env.CreateBuffer("c.go", `package consts
const a = 3`)
env.Await(
env.DiagnosticAtRegexp("a.go", "a = 1"),
env.DiagnosticAtRegexp("b.go", "a = 2"),
env.DiagnosticAtRegexp("c.go", "a = 3"),
)
env.CloseBuffer("c.go")
env.Await(
env.DiagnosticAtRegexp("a.go", "a = 1"),
env.DiagnosticAtRegexp("b.go", "a = 2"),
EmptyDiagnostics("c.go"),
)
})
}
// Tests golang/go#37978.
func TestIssue37978(t *testing.T) {
Run(t, exampleProgram, func(t *testing.T, env *Env) {
// Create a new workspace-level directory and empty file.
env.CreateBuffer("c/c.go", "")
// Write the file contents with a missing import.
env.EditBuffer("c/c.go", fake.Edit{
Text: `package c
const a = http.MethodGet
`,
})
env.Await(
env.DiagnosticAtRegexp("c/c.go", "http.MethodGet"),
)
// Save file, which will organize imports, adding the expected import.
// Expect the diagnostics to clear.
env.SaveBuffer("c/c.go")
env.Await(
EmptyDiagnostics("c/c.go"),
)
})
}
// Tests golang/go#38878: good a.go, bad a_test.go, remove a_test.go but its errors remain
// If the file is open in the editor, this is working as intended
// If the file is not open in the editor, the errors go away
const test38878 = `
-- go.mod --
module foo
go 1.12
-- a.go --
package x
// import "fmt"
func f() {}
-- a_test.go --
package x
import "testing"
func TestA(t *testing.T) {
f(3)
}
`
// Tests golang/go#38878: deleting a test file should clear its errors, and
// not break the workspace.
func TestDeleteTestVariant(t *testing.T) {
Run(t, test38878, func(t *testing.T, env *Env) {
env.Await(env.DiagnosticAtRegexp("a_test.go", `f\((3)\)`))
env.RemoveWorkspaceFile("a_test.go")
env.Await(EmptyDiagnostics("a_test.go"))
// Make sure the test variant has been removed from the workspace by
// triggering a metadata load.
env.OpenFile("a.go")
env.RegexpReplace("a.go", `// import`, "import")
env.Await(env.DiagnosticAtRegexp("a.go", `"fmt"`))
})
}
// Tests golang/go#38878: deleting a test file on disk while it's still open
// should not clear its errors.
func TestDeleteTestVariant_DiskOnly(t *testing.T) {
Run(t, test38878, func(t *testing.T, env *Env) {
env.OpenFile("a_test.go")
env.Await(DiagnosticAt("a_test.go", 5, 3))
env.Sandbox.Workdir.RemoveFile(context.Background(), "a_test.go")
env.Await(OnceMet(
env.DoneWithChangeWatchedFiles(),
DiagnosticAt("a_test.go", 5, 3)))
})
}
// TestNoMod confirms that gopls continues to work when a user adds a go.mod
// file to their workspace.
func TestNoMod(t *testing.T) {
const noMod = `
-- main.go --
package main
import "mod.com/bob"
func main() {
bob.Hello()
}
-- bob/bob.go --
package bob
func Hello() {
var x int
}
`
t.Run("manual", func(t *testing.T) {
Run(t, noMod, func(t *testing.T, env *Env) {
env.Await(
env.DiagnosticAtRegexp("main.go", `"mod.com/bob"`),
)
env.CreateBuffer("go.mod", `module mod.com
go 1.12
`)
env.SaveBuffer("go.mod")
env.Await(
EmptyDiagnostics("main.go"),
)
var d protocol.PublishDiagnosticsParams
env.Await(
OnceMet(
env.DiagnosticAtRegexp("bob/bob.go", "x"),
ReadDiagnostics("bob/bob.go", &d),
),
)
if len(d.Diagnostics) != 1 {
t.Fatalf("expected 1 diagnostic, got %v", len(d.Diagnostics))
}
})
})
t.Run("initialized", func(t *testing.T) {
Run(t, noMod, func(t *testing.T, env *Env) {
env.Await(
env.DiagnosticAtRegexp("main.go", `"mod.com/bob"`),
)
env.RunGoCommand("mod", "init", "mod.com")
env.Await(
EmptyDiagnostics("main.go"),
env.DiagnosticAtRegexp("bob/bob.go", "x"),
)
})
})
t.Run("without workspace module", func(t *testing.T) {
WithOptions(
Modes(Default),
).Run(t, noMod, func(t *testing.T, env *Env) {
env.Await(
env.DiagnosticAtRegexp("main.go", `"mod.com/bob"`),
)
if err := env.Sandbox.RunGoCommand(env.Ctx, "", "mod", []string{"init", "mod.com"}, true); err != nil {
t.Fatal(err)
}
env.Await(
EmptyDiagnostics("main.go"),
env.DiagnosticAtRegexp("bob/bob.go", "x"),
)
})
})
}
// Tests golang/go#38267.
func TestIssue38267(t *testing.T) {
const testPackage = `
-- go.mod --
module mod.com
go 1.12
-- lib.go --
package lib
func Hello(x string) {
_ = x
}
-- lib_test.go --
package lib
import "testing"
type testStruct struct{
name string
}
func TestHello(t *testing.T) {
testStructs := []*testStruct{
&testStruct{"hello"},
&testStruct{"goodbye"},
}
for y := range testStructs {
_ = y
}
}
`
Run(t, testPackage, func(t *testing.T, env *Env) {
env.OpenFile("lib_test.go")
env.Await(
DiagnosticAt("lib_test.go", 10, 2),
DiagnosticAt("lib_test.go", 11, 2),
)
env.OpenFile("lib.go")
env.RegexpReplace("lib.go", "_ = x", "var y int")
env.Await(
env.DiagnosticAtRegexp("lib.go", "y int"),
EmptyDiagnostics("lib_test.go"),
)
})
}
// Tests golang/go#38328.
func TestPackageChange_Issue38328(t *testing.T) {
const packageChange = `
-- go.mod --
module fake
go 1.12
-- a.go --
package foo
func main() {}
`
Run(t, packageChange, func(t *testing.T, env *Env) {
env.OpenFile("a.go")
env.RegexpReplace("a.go", "foo", "foox")
env.Await(
// When the bug reported in #38328 was present, we didn't get erroneous
// file diagnostics until after the didChange message generated by the
// package renaming was fully processed. Therefore, in order for this
// test to actually exercise the bug, we must wait until that work has
// completed.
OnceMet(
env.DoneWithChange(),
EmptyDiagnostics("a.go"),
),
)
})
}
const testPackageWithRequire = `
-- go.mod --
module mod.com
go 1.12
require foo.test v1.2.3
-- go.sum --
foo.test v1.2.3 h1:TMA+lyd1ck0TqjSFpNe4T6cf/K6TYkoHwOOcMBMjaEw=
foo.test v1.2.3/go.mod h1:Ij3kyLIe5lzjycjh13NL8I2gX0quZuTdW0MnmlwGBL4=
-- print.go --
package lib
import (
"fmt"
"foo.test/bar"
)
func PrintAnswer() {
fmt.Printf("answer: %s", bar.Answer)
}
`
const testPackageWithRequireProxy = `
-- foo.test@v1.2.3/go.mod --
module foo.test
go 1.12
-- foo.test@v1.2.3/bar/const.go --
package bar
const Answer = 42
`
func TestResolveDiagnosticWithDownload(t *testing.T) {
WithOptions(
ProxyFiles(testPackageWithRequireProxy),
).Run(t, testPackageWithRequire, func(t *testing.T, env *Env) {
env.OpenFile("print.go")
// Check that gopackages correctly loaded this dependency. We should get a
// diagnostic for the wrong formatting type.
// TODO: we should be able to easily also match the diagnostic message.
env.Await(env.DiagnosticAtRegexp("print.go", "fmt.Printf"))
})
}
func TestMissingDependency(t *testing.T) {
Run(t, testPackageWithRequire, func(t *testing.T, env *Env) {
env.OpenFile("print.go")
env.Await(LogMatching(protocol.Error, "initial workspace load failed", 1, false))
})
}
// Tests golang/go#36951.
func TestAdHocPackages_Issue36951(t *testing.T) {
const adHoc = `
-- b/b.go --
package b
func Hello() {
var x int
}
`
Run(t, adHoc, func(t *testing.T, env *Env) {
env.OpenFile("b/b.go")
env.Await(env.DiagnosticAtRegexp("b/b.go", "x"))
})
}
// Tests golang/go#37984: GOPATH should be read from the go command.
func TestNoGOPATH_Issue37984(t *testing.T) {
const files = `
-- main.go --
package main
func _() {
fmt.Println("Hello World")
}
`
WithOptions(
EnvVars{
"GOPATH": "",
"GO111MODULE": "off",
},
).Run(t, files, func(t *testing.T, env *Env) {
env.OpenFile("main.go")
env.Await(env.DiagnosticAtRegexp("main.go", "fmt"))
env.SaveBuffer("main.go")
env.Await(EmptyDiagnostics("main.go"))
})
}
// Tests golang/go#38669.
func TestEqualInEnv_Issue38669(t *testing.T) {
const files = `
-- go.mod --
module mod.com
go 1.12
-- main.go --
package main
var _ = x.X
-- x/x.go --
package x
var X = 0
`
WithOptions(
EnvVars{"GOFLAGS": "-tags=foo"},
).Run(t, files, func(t *testing.T, env *Env) {
env.OpenFile("main.go")
env.OrganizeImports("main.go")
env.Await(EmptyDiagnostics("main.go"))
})
}
// Tests golang/go#38467.
func TestNoSuggestedFixesForGeneratedFiles_Issue38467(t *testing.T) {
const generated = `
-- go.mod --
module mod.com
go 1.12
-- main.go --
package main
// Code generated by generator.go. DO NOT EDIT.
func _() {
for i, _ := range []string{} {
_ = i
}
}
`
Run(t, generated, func(t *testing.T, env *Env) {
env.OpenFile("main.go")
var d protocol.PublishDiagnosticsParams
env.Await(
OnceMet(
DiagnosticAt("main.go", 5, 8),
ReadDiagnostics("main.go", &d),
),
)
if fixes := env.GetQuickFixes("main.go", d.Diagnostics); len(fixes) != 0 {
t.Errorf("got quick fixes %v, wanted none", fixes)
}
})
}
// Expect a module/GOPATH error if there is an error in the file at startup.
// Tests golang/go#37279.
func TestBrokenWorkspace_OutsideModule(t *testing.T) {
const noModule = `
-- a.go --
package foo
import "mod.com/hello"
func f() {
hello.Goodbye()
}
`
Run(t, noModule, func(t *testing.T, env *Env) {
env.OpenFile("a.go")
env.Await(
// Expect the adHocPackagesWarning.
OutstandingWork(lsp.WorkspaceLoadFailure, "outside of a module"),
)
// Deleting the import dismisses the warning.
env.RegexpReplace("a.go", `import "mod.com/hello"`, "")
env.Await(
NoOutstandingWork(),
)
})
}
func TestNonGoFolder(t *testing.T) {
const files = `
-- hello.txt --
hi mom
`
for _, go111module := range []string{"on", "off", ""} {
t.Run(fmt.Sprintf("GO111MODULE_%v", go111module), func(t *testing.T) {
WithOptions(
EnvVars{"GO111MODULE": go111module},
).Run(t, files, func(t *testing.T, env *Env) {
env.Await(
NoOutstandingWork(),
)
})
})
}
}
// Tests the repro case from golang/go#38602. Diagnostics are now handled properly,
// which blocks type checking.
func TestConflictingMainPackageErrors(t *testing.T) {
const collision = `
-- x/x.go --
package x
import "x/hello"
func Hello() {
hello.HiThere()
}
-- x/main.go --
package main
func main() {
fmt.Println("")
}
`
WithOptions(
InGOPATH(),
EnvVars{"GO111MODULE": "off"},
).Run(t, collision, func(t *testing.T, env *Env) {
env.OpenFile("x/x.go")
env.Await(
env.DiagnosticAtRegexpWithMessage("x/x.go", `^`, "found packages main (main.go) and x (x.go)"),
env.DiagnosticAtRegexpWithMessage("x/main.go", `^`, "found packages main (main.go) and x (x.go)"),
)
// We don't recover cleanly from the errors without good overlay support.
if testenv.Go1Point() >= 16 {
env.RegexpReplace("x/x.go", `package x`, `package main`)
env.Await(OnceMet(
env.DoneWithChange(),
env.DiagnosticAtRegexp("x/main.go", `fmt`)))
}
})
}
const ardanLabsProxy = `
-- github.com/ardanlabs/conf@v1.2.3/go.mod --
module github.com/ardanlabs/conf
go 1.12
-- github.com/ardanlabs/conf@v1.2.3/conf.go --
package conf
var ErrHelpWanted error
`
// Test for golang/go#38211.
func Test_Issue38211(t *testing.T) {
const ardanLabs = `
-- go.mod --
module mod.com
go 1.14
-- main.go --
package main
import "github.com/ardanlabs/conf"
func main() {
_ = conf.ErrHelpWanted
}
`
WithOptions(
ProxyFiles(ardanLabsProxy),
).Run(t, ardanLabs, func(t *testing.T, env *Env) {
// Expect a diagnostic with a suggested fix to add
// "github.com/ardanlabs/conf" to the go.mod file.
env.OpenFile("go.mod")
env.OpenFile("main.go")
var d protocol.PublishDiagnosticsParams
env.Await(
OnceMet(
env.DiagnosticAtRegexp("main.go", `"github.com/ardanlabs/conf"`),
ReadDiagnostics("main.go", &d),
),
)
env.ApplyQuickFixes("main.go", d.Diagnostics)
env.SaveBuffer("go.mod")
env.Await(
EmptyDiagnostics("main.go"),
)
// Comment out the line that depends on conf and expect a
// diagnostic and a fix to remove the import.
env.RegexpReplace("main.go", "_ = conf.ErrHelpWanted", "//_ = conf.ErrHelpWanted")
env.Await(
env.DiagnosticAtRegexp("main.go", `"github.com/ardanlabs/conf"`),
)
env.SaveBuffer("main.go")
// Expect a diagnostic and fix to remove the dependency in the go.mod.
env.Await(EmptyDiagnostics("main.go"))
env.Await(
OnceMet(
env.DiagnosticAtRegexpWithMessage("go.mod", "require github.com/ardanlabs/conf", "not used in this module"),
ReadDiagnostics("go.mod", &d),
),
)
env.ApplyQuickFixes("go.mod", d.Diagnostics)
env.SaveBuffer("go.mod")
env.Await(
EmptyDiagnostics("go.mod"),
)
// Uncomment the lines and expect a new diagnostic for the import.
env.RegexpReplace("main.go", "//_ = conf.ErrHelpWanted", "_ = conf.ErrHelpWanted")
env.SaveBuffer("main.go")
env.Await(
env.DiagnosticAtRegexp("main.go", `"github.com/ardanlabs/conf"`),
)
})
}
// Test for golang/go#38207.
func TestNewModule_Issue38207(t *testing.T) {
const emptyFile = `
-- go.mod --
module mod.com
go 1.12
-- main.go --
`
WithOptions(
ProxyFiles(ardanLabsProxy),
).Run(t, emptyFile, func(t *testing.T, env *Env) {
env.CreateBuffer("main.go", `package main
import "github.com/ardanlabs/conf"
func main() {
_ = conf.ErrHelpWanted
}
`)
env.SaveBuffer("main.go")
var d protocol.PublishDiagnosticsParams
env.Await(
OnceMet(
env.DiagnosticAtRegexpWithMessage("main.go", `"github.com/ardanlabs/conf"`, "no required module"),
ReadDiagnostics("main.go", &d),
),
)
env.ApplyQuickFixes("main.go", d.Diagnostics)
env.Await(
EmptyDiagnostics("main.go"),
)
})
}
// Test for golang/go#36960.
func TestNewFileBadImports_Issue36960(t *testing.T) {
const simplePackage = `
-- go.mod --
module mod.com
go 1.14
-- a/a1.go --
package a
import "fmt"
func _() {
fmt.Println("hi")
}
`
Run(t, simplePackage, func(t *testing.T, env *Env) {
env.OpenFile("a/a1.go")
env.CreateBuffer("a/a2.go", ``)
env.SaveBufferWithoutActions("a/a2.go")
// We can't use OnceMet here (at least, not easily) because the didSave
// races with the didChangeWatchedFiles.
//
// TODO(rfindley): add an AllOf expectation combinator, or an expectation
// that all notifications have been processed.
env.Await(
EmptyDiagnostics("a/a1.go"),
)
env.EditBuffer("a/a2.go", fake.NewEdit(0, 0, 0, 0, `package a`))
env.Await(
OnceMet(
env.DoneWithChange(),
EmptyDiagnostics("a/a1.go"),
),
)
})
}
// This test tries to replicate the workflow of a user creating a new x test.
// It also tests golang/go#39315.
func TestManuallyCreatingXTest(t *testing.T) {
// Create a package that already has a test variant (in-package test).
const testVariant = `
-- go.mod --
module mod.com
go 1.15
-- hello/hello.go --
package hello
func Hello() {
var x int
}
-- hello/hello_test.go --
package hello
import "testing"
func TestHello(t *testing.T) {
var x int
Hello()
}
`
Run(t, testVariant, func(t *testing.T, env *Env) {
// Open the file, triggering the workspace load.
// There are errors in the code to ensure all is working as expected.
env.OpenFile("hello/hello.go")
env.Await(
env.DiagnosticAtRegexp("hello/hello.go", "x"),
env.DiagnosticAtRegexp("hello/hello_test.go", "x"),
)
// Create an empty file with the intention of making it an x test.
// This resembles a typical flow in an editor like VS Code, in which
// a user would create an empty file and add content, saving
// intermittently.
// TODO(rstambler): There might be more edge cases here, as file
// content can be added incrementally.
env.CreateBuffer("hello/hello_x_test.go", ``)
// Save the empty file (no actions since formatting will fail).
env.SaveBufferWithoutActions("hello/hello_x_test.go")
// Add the content. The missing import is for the package under test.
env.EditBuffer("hello/hello_x_test.go", fake.NewEdit(0, 0, 0, 0, `package hello_test
import (
"testing"
)
func TestHello(t *testing.T) {
hello.Hello()
}
`))
// Expect a diagnostic for the missing import. Save, which should
// trigger import organization. The diagnostic should clear.
env.Await(
env.DiagnosticAtRegexp("hello/hello_x_test.go", "hello.Hello"),
)
env.SaveBuffer("hello/hello_x_test.go")
env.Await(
EmptyDiagnostics("hello/hello_x_test.go"),
)
})
}
// Reproduce golang/go#40690.
func TestCreateOnlyXTest(t *testing.T) {
const mod = `
-- go.mod --
module mod.com
go 1.12
-- foo/foo.go --
package foo
-- foo/bar_test.go --
`
Run(t, mod, func(t *testing.T, env *Env) {
env.OpenFile("foo/bar_test.go")
env.EditBuffer("foo/bar_test.go", fake.NewEdit(0, 0, 0, 0, "package foo"))
env.Await(env.DoneWithChange())
env.RegexpReplace("foo/bar_test.go", "package foo", `package foo_test
import "testing"
func TestX(t *testing.T) {
var x int
}
`)
env.Await(
env.DiagnosticAtRegexp("foo/bar_test.go", "x"),
)
})
}
func TestChangePackageName(t *testing.T) {
const mod = `
-- go.mod --
module mod.com
go 1.12
-- foo/foo.go --
package foo
-- foo/bar_test.go --
package foo_
`
Run(t, mod, func(t *testing.T, env *Env) {
env.OpenFile("foo/bar_test.go")
env.RegexpReplace("foo/bar_test.go", "package foo_", "package foo_test")
env.Await(
OnceMet(
env.DoneWithChange(),
EmptyOrNoDiagnostics("foo/bar_test.go"),
EmptyOrNoDiagnostics("foo/foo.go"),
),
)
})
}
func TestIgnoredFiles(t *testing.T) {
const ws = `
-- go.mod --
module mod.com
go 1.12
-- _foo/x.go --
package x
var _ = foo.Bar
`
Run(t, ws, func(t *testing.T, env *Env) {
env.OpenFile("_foo/x.go")
env.Await(
OnceMet(
env.DoneWithOpen(),
EmptyDiagnostics("_foo/x.go"),
))
})
}
// Partially reproduces golang/go#38977, moving a file between packages.
// It also gets hit by some go command bug fixed in 1.15, but we don't
// care about that so much here.
func TestDeletePackage(t *testing.T) {
const ws = `
-- go.mod --
module mod.com
go 1.15
-- a/a.go --
package a
const A = 1
-- b/b.go --
package b
import "mod.com/a"
const B = a.A
-- c/c.go --
package c
import "mod.com/a"
const C = a.A
`
Run(t, ws, func(t *testing.T, env *Env) {
env.OpenFile("b/b.go")
env.Await(env.DoneWithOpen())
// Delete c/c.go, the only file in package c.
env.RemoveWorkspaceFile("c/c.go")
// We should still get diagnostics for files that exist.
env.RegexpReplace("b/b.go", `a.A`, "a.Nonexistant")
env.Await(env.DiagnosticAtRegexp("b/b.go", `Nonexistant`))
})
}
// This is a copy of the scenario_default/quickfix_empty_files.txt test from
// govim. Reproduces golang/go#39646.
func TestQuickFixEmptyFiles(t *testing.T) {
const mod = `
-- go.mod --
module mod.com
go 1.12
`
// To fully recreate the govim tests, we create files by inserting
// a newline, adding to the file, and then deleting the newline.
// Wait for each event to process to avoid cancellations and force
// package loads.
writeGoVim := func(env *Env, name, content string) {
env.WriteWorkspaceFile(name, "")
env.Await(env.DoneWithChangeWatchedFiles())
env.CreateBuffer(name, "\n")
env.Await(env.DoneWithOpen())
env.EditBuffer(name, fake.NewEdit(1, 0, 1, 0, content))
env.Await(env.DoneWithChange())
env.EditBuffer(name, fake.NewEdit(0, 0, 1, 0, ""))
env.Await(env.DoneWithChange())
}
const p = `package p; func DoIt(s string) {};`
const main = `package main
import "mod.com/p"
func main() {
p.DoIt(5)
}
`
// A simple version of the test that reproduces most of the problems it
// exposes.
t.Run("short", func(t *testing.T) {
Run(t, mod, func(t *testing.T, env *Env) {
writeGoVim(env, "p/p.go", p)
writeGoVim(env, "main.go", main)
env.Await(env.DiagnosticAtRegexp("main.go", "5"))
})
})
// A full version that replicates the whole flow of the test.
t.Run("full", func(t *testing.T) {
Run(t, mod, func(t *testing.T, env *Env) {
writeGoVim(env, "p/p.go", p)
writeGoVim(env, "main.go", main)
writeGoVim(env, "p/p_test.go", `package p
import "testing"
func TestDoIt(t *testing.T) {
DoIt(5)
}
`)
writeGoVim(env, "p/x_test.go", `package p_test
import (
"testing"
"mod.com/p"
)
func TestDoIt(t *testing.T) {
p.DoIt(5)
}
`)
env.Await(
env.DiagnosticAtRegexp("main.go", "5"),
env.DiagnosticAtRegexp("p/p_test.go", "5"),
env.DiagnosticAtRegexp("p/x_test.go", "5"),
)
env.RegexpReplace("p/p.go", "s string", "i int")
env.Await(
EmptyDiagnostics("main.go"),
EmptyDiagnostics("p/p_test.go"),
EmptyDiagnostics("p/x_test.go"),
)
})
})
}
func TestSingleFile(t *testing.T) {
const mod = `
-- go.mod --
module mod.com
go 1.13
-- a/a.go --
package a
func _() {
var x int
}
`
WithOptions(
// Empty workspace folders.
WorkspaceFolders(),
).Run(t, mod, func(t *testing.T, env *Env) {
env.OpenFile("a/a.go")
env.Await(
env.DiagnosticAtRegexp("a/a.go", "x"),
)
})
}
// Reproduces the case described in
// https://github.com/golang/go/issues/39296#issuecomment-652058883.
func TestPkgm(t *testing.T) {
const basic = `
-- go.mod --
module mod.com
go 1.15
-- foo/foo.go --
package foo
import "fmt"
func Foo() {
fmt.Println("")
}
`
Run(t, basic, func(t *testing.T, env *Env) {
env.WriteWorkspaceFile("foo/foo_test.go", `package main
func main() {
}`)
env.OpenFile("foo/foo_test.go")
env.RegexpReplace("foo/foo_test.go", `package main`, `package foo`)
env.Await(
OnceMet(
env.DoneWithChange(),
NoDiagnostics("foo/foo.go"),
),
)
})
}
func TestClosingBuffer(t *testing.T) {
const basic = `
-- go.mod --
module mod.com
go 1.14
-- main.go --
package main
func main() {}
`
Run(t, basic, func(t *testing.T, env *Env) {
env.Editor.CreateBuffer(env.Ctx, "foo.go", `package main`)
env.Await(
env.DoneWithOpen(),
)
env.CloseBuffer("foo.go")
env.Await(
OnceMet(
env.DoneWithClose(),
NoLogMatching(protocol.Info, "packages=0"),
),
)
})
}
// Reproduces golang/go#38424.
func TestCutAndPaste(t *testing.T) {
const basic = `
-- go.mod --
module mod.com
go 1.14
-- main2.go --
package main
`
Run(t, basic, func(t *testing.T, env *Env) {
env.CreateBuffer("main.go", "")
env.Await(env.DoneWithOpen())
env.SaveBufferWithoutActions("main.go")
env.Await(env.DoneWithSave(), env.DoneWithChangeWatchedFiles())
env.EditBuffer("main.go", fake.NewEdit(0, 0, 0, 0, `package main
func main() {
}
`))
env.Await(env.DoneWithChange())
env.SaveBuffer("main.go")
env.Await(env.DoneWithSave(), env.DoneWithChangeWatchedFiles())
env.EditBuffer("main.go", fake.NewEdit(0, 0, 4, 0, ""))
env.Await(env.DoneWithChange())
env.EditBuffer("main.go", fake.NewEdit(0, 0, 0, 0, `package main
func main() {
var x int
}
`))
env.Await(
env.DiagnosticAtRegexp("main.go", "x"),
)
})
}
// Reproduces golang/go#39763.
func TestInvalidPackageName(t *testing.T) {
const pkgDefault = `
-- go.mod --
module mod.com
go 1.12
-- main.go --
package default
func main() {}
`
Run(t, pkgDefault, func(t *testing.T, env *Env) {
env.OpenFile("main.go")
env.Await(
env.DiagnosticAtRegexpWithMessage("main.go", "default", "expected 'IDENT'"),
)
})
}
// This tests the functionality of the "limitWorkspaceScope"
func TestLimitWorkspaceScope(t *testing.T) {
const mod = `
-- go.mod --
module mod.com
go 1.12
-- a/main.go --
package main
func main() {}
-- main.go --
package main
func main() {
var x int
}
`
WithOptions(
WorkspaceFolders("a"),
).Run(t, mod, func(t *testing.T, env *Env) {
env.OpenFile("a/main.go")
env.Await(
env.DiagnosticAtRegexp("main.go", "x"),
)
})
WithOptions(
WorkspaceFolders("a"),
Settings{"expandWorkspaceToModule": false},
).Run(t, mod, func(t *testing.T, env *Env) {
env.OpenFile("a/main.go")
env.Await(
NoDiagnostics("main.go"),
)
})
}
func TestSimplifyCompositeLitDiagnostic(t *testing.T) {
const files = `
-- go.mod --
module mod.com
go 1.12
-- main.go --
package main
import "fmt"
type t struct {
msg string
}
func main() {
x := []t{t{"msg"}}
fmt.Println(x)
}
`
WithOptions(
Settings{"staticcheck": true},
).Run(t, files, func(t *testing.T, env *Env) {
env.OpenFile("main.go")
var d protocol.PublishDiagnosticsParams
env.Await(OnceMet(
env.DiagnosticAtRegexpWithMessage("main.go", `t{"msg"}`, "redundant type"),
ReadDiagnostics("main.go", &d),
))
if tags := d.Diagnostics[0].Tags; len(tags) == 0 || tags[0] != protocol.Unnecessary {
t.Errorf("wanted Unnecessary tag on diagnostic, got %v", tags)
}
env.ApplyQuickFixes("main.go", d.Diagnostics)
env.Await(EmptyDiagnostics("main.go"))
})
}
// Test some secondary diagnostics
func TestSecondaryDiagnostics(t *testing.T) {
const dir = `
-- go.mod --
module mod.com
go 1.12
-- main.go --
package main
func main() {
panic("not here")
}
-- other.go --
package main
func main() {}
`
Run(t, dir, func(t *testing.T, env *Env) {
env.OpenFile("main.go")
env.OpenFile("other.go")
x := env.Awaiter.DiagnosticsFor("main.go")
if x == nil {
t.Fatalf("expected 1 diagnostic, got none")
}
if len(x.Diagnostics) != 1 {
t.Fatalf("main.go, got %d diagnostics, expected 1", len(x.Diagnostics))
}
keep := x.Diagnostics[0]
y := env.Awaiter.DiagnosticsFor("other.go")
if len(y.Diagnostics) != 1 {
t.Fatalf("other.go: got %d diagnostics, expected 1", len(y.Diagnostics))
}
if len(y.Diagnostics[0].RelatedInformation) != 1 {
t.Fatalf("got %d RelatedInformations, expected 1", len(y.Diagnostics[0].RelatedInformation))
}
// check that the RelatedInformation matches the error from main.go
c := y.Diagnostics[0].RelatedInformation[0]
if c.Location.Range != keep.Range {
t.Errorf("locations don't match. Got %v expected %v", c.Location.Range, keep.Range)
}
})
}
func TestNotifyOrphanedFiles(t *testing.T) {
const files = `
-- go.mod --
module mod.com
go 1.12
-- a/a.go --
package a
func main() {
var x int
}
-- a/a_exclude.go --
// +build exclude
package a
func _() {
var x int
}
`
Run(t, files, func(t *testing.T, env *Env) {
env.OpenFile("a/a.go")
env.Await(
env.DiagnosticAtRegexp("a/a.go", "x"),
)
env.OpenFile("a/a_exclude.go")
env.Await(
DiagnosticAt("a/a_exclude.go", 2, 8),
)
})
}
func TestEnableAllExperiments(t *testing.T) {
// Before the oldest supported Go version, gopls sends a warning to upgrade
// Go, which fails the expectation below.
testenv.NeedsGo1Point(t, lsp.OldestSupportedGoVersion())
const mod = `
-- go.mod --
module mod.com
go 1.12
-- main.go --
package main
import "bytes"
func b(c bytes.Buffer) {
_ = 1
}
`
WithOptions(
Settings{"allExperiments": true},
).Run(t, mod, func(t *testing.T, env *Env) {
// Confirm that the setting doesn't cause any warnings.
env.Await(
OnceMet(
InitialWorkspaceLoad,
NoShownMessage(""), // empty substring to match any message
),
)
})
}
func TestSwig(t *testing.T) {
// This is fixed in Go 1.17, but not earlier.
testenv.NeedsGo1Point(t, 17)
if _, err := exec.LookPath("swig"); err != nil {
t.Skip("skipping test: swig not available")
}
if _, err := exec.LookPath("g++"); err != nil {
t.Skip("skipping test: g++ not available")
}
const mod = `
-- go.mod --
module mod.com
go 1.12
-- pkg/simple/export_swig.go --
package simple
func ExportSimple(x, y int) int {
return Gcd(x, y)
}
-- pkg/simple/simple.swigcxx --
%module simple
%inline %{
extern int gcd(int x, int y)
{
int g;
g = y;
while (x > 0) {
g = x;
x = y % x;
y = g;
}
return g;
}
%}
-- main.go --
package a
func main() {
var x int
}
`
Run(t, mod, func(t *testing.T, env *Env) {
env.Await(
OnceMet(
InitialWorkspaceLoad,
NoDiagnosticWithMessage("", "illegal character U+0023 '#'"),
),
)
})
}
// When foo_test.go is opened, gopls will object to the borked package name.
// This test asserts that when the package name is fixed, gopls will soon after
// have no more complaints about it.
// https://github.com/golang/go/issues/41061
func TestRenamePackage(t *testing.T) {
const proxy = `
-- example.com@v1.2.3/go.mod --
module example.com
go 1.12
-- example.com@v1.2.3/blah/blah.go --
package blah
const Name = "Blah"
-- random.org@v1.2.3/go.mod --
module random.org
go 1.12
-- random.org@v1.2.3/blah/blah.go --
package hello
const Name = "Hello"
`
const contents = `
-- go.mod --
module mod.com
go 1.12
-- main.go --
package main
import "example.com/blah"
func main() {
blah.Hello()
}
-- bob.go --
package main
-- foo/foo.go --
package foo
-- foo/foo_test.go --
package foo_
`
WithOptions(
ProxyFiles(proxy),
InGOPATH(),
EnvVars{"GO111MODULE": "off"},
).Run(t, contents, func(t *testing.T, env *Env) {
// Simulate typing character by character.
env.OpenFile("foo/foo_test.go")
env.Await(env.DoneWithOpen())
env.RegexpReplace("foo/foo_test.go", "_", "_t")
env.Await(env.DoneWithChange())
env.RegexpReplace("foo/foo_test.go", "_t", "_test")
env.Await(env.DoneWithChange())
env.Await(
EmptyDiagnostics("foo/foo_test.go"),
NoOutstandingWork(),
)
})
}
// TestProgressBarErrors confirms that critical workspace load errors are shown
// and updated via progress reports.
func TestProgressBarErrors(t *testing.T) {
const pkg = `
-- go.mod --
modul mod.com
go 1.12
-- main.go --
package main
`
Run(t, pkg, func(t *testing.T, env *Env) {
env.OpenFile("go.mod")
env.Await(
OutstandingWork(lsp.WorkspaceLoadFailure, "unknown directive"),
)
env.EditBuffer("go.mod", fake.NewEdit(0, 0, 3, 0, `module mod.com
go 1.hello
`))
// As of golang/go#42529, go.mod changes do not reload the workspace until
// they are saved.
env.SaveBufferWithoutActions("go.mod")
env.Await(
OutstandingWork(lsp.WorkspaceLoadFailure, "invalid go version"),
)
env.RegexpReplace("go.mod", "go 1.hello", "go 1.12")
env.SaveBufferWithoutActions("go.mod")
env.Await(
NoOutstandingWork(),
)
})
}
func TestDeleteDirectory(t *testing.T) {
const mod = `
-- bob/bob.go --
package bob
func Hello() {
var x int
}
-- go.mod --
module mod.com
-- cmd/main.go --
package main
import "mod.com/bob"
func main() {
bob.Hello()
}
`
Run(t, mod, func(t *testing.T, env *Env) {
env.Await(FileWatchMatching("bob"))
env.RemoveWorkspaceFile("bob")
env.Await(
env.DiagnosticAtRegexp("cmd/main.go", `"mod.com/bob"`),
EmptyDiagnostics("bob/bob.go"),
NoFileWatchMatching("bob"),
)
})
}
// Confirms that circular imports are tested and reported.
func TestCircularImports(t *testing.T) {
const mod = `
-- go.mod --
module mod.com
go 1.12
-- self/self.go --
package self
import _ "mod.com/self"
func Hello() {}
-- double/a/a.go --
package a
import _ "mod.com/double/b"
-- double/b/b.go --
package b
import _ "mod.com/double/a"
-- triple/a/a.go --
package a
import _ "mod.com/triple/b"
-- triple/b/b.go --
package b
import _ "mod.com/triple/c"
-- triple/c/c.go --
package c
import _ "mod.com/triple/a"
`
Run(t, mod, func(t *testing.T, env *Env) {
env.Await(
env.DiagnosticAtRegexpWithMessage("self/self.go", `_ "mod.com/self"`, "import cycle not allowed"),
env.DiagnosticAtRegexpWithMessage("double/a/a.go", `_ "mod.com/double/b"`, "import cycle not allowed"),
env.DiagnosticAtRegexpWithMessage("triple/a/a.go", `_ "mod.com/triple/b"`, "import cycle not allowed"),
)
})
}
// Tests golang/go#46667: deleting a problematic import path should resolve
// import cycle errors.
func TestResolveImportCycle(t *testing.T) {
const mod = `
-- go.mod --
module mod.test
go 1.16
-- a/a.go --
package a
import "mod.test/b"
const A = b.A
const B = 2
-- b/b.go --
package b
import "mod.test/a"
const A = 1
const B = a.B
`
Run(t, mod, func(t *testing.T, env *Env) {
env.OpenFile("a/a.go")
env.OpenFile("b/b.go")
env.Await(
OnceMet(
env.DoneWithOpen(),
// The Go command sometimes tells us about only one of the import cycle
// errors below. For robustness of this test, succeed if we get either.
//
// TODO(golang/go#52904): we should get *both* of these errors.
AnyOf(
env.DiagnosticAtRegexpWithMessage("a/a.go", `"mod.test/b"`, "import cycle"),
env.DiagnosticAtRegexpWithMessage("b/b.go", `"mod.test/a"`, "import cycle"),
),
),
)
env.RegexpReplace("b/b.go", `const B = a\.B`, "")
env.SaveBuffer("b/b.go")
env.Await(
EmptyOrNoDiagnostics("a/a.go"),
EmptyOrNoDiagnostics("b/b.go"),
)
})
}
func TestBadImport(t *testing.T) {
const mod = `
-- go.mod --
module mod.com
go 1.12
-- main.go --
package main
import (
_ "nosuchpkg"
)
`
t.Run("module", func(t *testing.T) {
Run(t, mod, func(t *testing.T, env *Env) {
env.Await(
env.DiagnosticAtRegexpWithMessage("main.go", `"nosuchpkg"`, `could not import nosuchpkg (no required module provides package "nosuchpkg"`),
)
})
})
t.Run("GOPATH", func(t *testing.T) {
WithOptions(
InGOPATH(),
EnvVars{"GO111MODULE": "off"},
Modes(Default),
).Run(t, mod, func(t *testing.T, env *Env) {
env.Await(
env.DiagnosticAtRegexpWithMessage("main.go", `"nosuchpkg"`, `cannot find package "nosuchpkg" in any of`),
)
})
})
}
func TestNestedModules(t *testing.T) {
const proxy = `
-- nested.com@v1.0.0/go.mod --
module nested.com
go 1.12
-- nested.com@v1.0.0/hello/hello.go --
package hello
func Hello() {}
`
const nested = `
-- go.mod --
module mod.com
go 1.12
require nested.com v1.0.0
-- go.sum --
nested.com v1.0.0 h1:I6spLE4CgFqMdBPc+wTV2asDO2QJ3tU0YAT+jkLeN1I=
nested.com v1.0.0/go.mod h1:ly53UzXQgVjSlV7wicdBB4p8BxfytuGT1Xcyv0ReJfI=
-- main.go --
package main
import "nested.com/hello"
func main() {
hello.Hello()
}
-- nested/go.mod --
module nested.com
-- nested/hello/hello.go --
package hello
func Hello() {
helloHelper()
}
-- nested/hello/hello_helper.go --
package hello
func helloHelper() {}
`
WithOptions(
ProxyFiles(proxy),
Modes(Default),
).Run(t, nested, func(t *testing.T, env *Env) {
// Expect a diagnostic in a nested module.
env.OpenFile("nested/hello/hello.go")
didOpen := env.DoneWithOpen()
env.Await(
OnceMet(
didOpen,
env.DiagnosticAtRegexp("nested/hello/hello.go", "helloHelper"),
),
OnceMet(
didOpen,
env.DiagnosticAtRegexpWithMessage("nested/hello/hello.go", "package hello", "nested module"),
),
OnceMet(
didOpen,
OutstandingWork(lsp.WorkspaceLoadFailure, "nested module"),
),
)
})
}
func TestAdHocPackagesReloading(t *testing.T) {
const nomod = `
-- main.go --
package main
func main() {}
`
Run(t, nomod, func(t *testing.T, env *Env) {
env.OpenFile("main.go")
env.RegexpReplace("main.go", "{}", "{ var x int; }") // simulate typing
env.Await(
OnceMet(
env.DoneWithChange(),
NoLogMatching(protocol.Info, "packages=1"),
),
)
})
}
func TestBuildTagChange(t *testing.T) {
const files = `
-- go.mod --
module mod.com
go 1.12
-- foo.go --
// decoy comment
// +build hidden
// decoy comment
package foo
var Foo = 1
-- bar.go --
package foo
var Bar = Foo
`
Run(t, files, func(t *testing.T, env *Env) {
env.OpenFile("foo.go")
env.Await(env.DiagnosticAtRegexp("bar.go", `Foo`))
env.RegexpReplace("foo.go", `\+build`, "")
env.Await(EmptyDiagnostics("bar.go"))
})
}
func TestIssue44736(t *testing.T) {
const files = `
-- go.mod --
module blah.com
go 1.16
-- main.go --
package main
import "fmt"
func main() {
asdf
fmt.Printf("This is a test %v")
fdas
}
-- other.go --
package main
`
Run(t, files, func(t *testing.T, env *Env) {
env.OpenFile("main.go")
env.OpenFile("other.go")
env.Await(
env.DiagnosticAtRegexp("main.go", "asdf"),
env.DiagnosticAtRegexp("main.go", "fdas"),
)
env.SetBufferContent("other.go", "package main\n\nasdf")
// The new diagnostic in other.go should not suppress diagnostics in main.go.
env.Await(
OnceMet(
env.DiagnosticAtRegexpWithMessage("other.go", "asdf", "expected declaration"),
env.DiagnosticAtRegexp("main.go", "asdf"),
),
)
})
}
func TestInitialization(t *testing.T) {
const files = `
-- go.mod --
module mod.com
go 1.16
-- main.go --
package main
`
Run(t, files, func(t *testing.T, env *Env) {
env.OpenFile("go.mod")
env.Await(env.DoneWithOpen())
env.RegexpReplace("go.mod", "module", "modul")
env.SaveBufferWithoutActions("go.mod")
env.Await(
OnceMet(
env.DoneWithSave(),
NoLogMatching(protocol.Error, "initial workspace load failed"),
),
)
})
}
// This test confirms that the view does not reinitialize when a go.mod file is
// opened.
func TestNoReinitialize(t *testing.T) {
const files = `
-- go.mod --
module mod.com
go 1.12
-- main.go --
package main
func main() {}
`
Run(t, files, func(t *testing.T, env *Env) {
env.OpenFile("go.mod")
env.Await(
OnceMet(
env.DoneWithOpen(),
LogMatching(protocol.Info, `.*query=\[builtin mod.com/...\].*`, 1, false),
),
)
})
}
func TestLangVersion(t *testing.T) {
testenv.NeedsGo1Point(t, 18) // Requires types.Config.GoVersion, new in 1.18.
const files = `
-- go.mod --
module mod.com
go 1.12
-- main.go --
package main
const C = 0b10
`
Run(t, files, func(t *testing.T, env *Env) {
env.Await(env.DiagnosticAtRegexpWithMessage("main.go", `0b10`, "go1.13 or later"))
env.WriteWorkspaceFile("go.mod", "module mod.com \n\ngo 1.13\n")
env.Await(EmptyDiagnostics("main.go"))
})
}
func TestNoQuickFixForUndeclaredConstraint(t *testing.T) {
testenv.NeedsGo1Point(t, 18)
const files = `
-- go.mod --
module mod.com
go 1.18
-- main.go --
package main
func F[T C](_ T) {
}
`
Run(t, files, func(t *testing.T, env *Env) {
var d protocol.PublishDiagnosticsParams
env.Await(
OnceMet(
env.DiagnosticAtRegexp("main.go", `C`),
ReadDiagnostics("main.go", &d),
),
)
if fixes := env.GetQuickFixes("main.go", d.Diagnostics); len(fixes) != 0 {
t.Errorf("got quick fixes %v, wanted none", fixes)
}
})
}
func TestEditGoDirective(t *testing.T) {
testenv.NeedsGo1Point(t, 18)
const files = `
-- go.mod --
module mod.com
go 1.16
-- main.go --
package main
func F[T any](_ T) {
}
`
Run(t, files, func(_ *testing.T, env *Env) { // Create a new workspace-level directory and empty file.
var d protocol.PublishDiagnosticsParams
env.Await(
OnceMet(
env.DiagnosticAtRegexpWithMessage("main.go", `T any`, "type parameter"),
ReadDiagnostics("main.go", &d),
),
)
env.ApplyQuickFixes("main.go", d.Diagnostics)
env.Await(
EmptyDiagnostics("main.go"),
)
})
}
func TestEditGoDirectiveWorkspace(t *testing.T) {
testenv.NeedsGo1Point(t, 18)
const files = `
-- go.mod --
module mod.com
go 1.16
-- go.work --
go 1.18
use .
-- main.go --
package main
func F[T any](_ T) {
}
`
Run(t, files, func(_ *testing.T, env *Env) { // Create a new workspace-level directory and empty file.
var d protocol.PublishDiagnosticsParams
// Once the initial workspace load is complete, we should have a diagnostic
// because generics are not supported at 1.16.
env.Await(
OnceMet(
InitialWorkspaceLoad,
env.DiagnosticAtRegexpWithMessage("main.go", `T any`, "type parameter"),
ReadDiagnostics("main.go", &d),
),
)
// This diagnostic should have a quick fix to edit the go version.
env.ApplyQuickFixes("main.go", d.Diagnostics)
// Once the edit is applied, the problematic diagnostics should be
// resolved.
env.Await(
OnceMet(
env.DoneWithChangeWatchedFiles(), // go.mod should have been quick-fixed
EmptyDiagnostics("main.go"),
),
)
})
}
// This test demonstrates that analysis facts are correctly propagated
// across packages.
func TestInterpackageAnalysis(t *testing.T) {
const src = `
-- go.mod --
module example.com
-- a/a.go --
package a
import "example.com/b"
func _() {
new(b.B).Printf("%d", "s") // printf error
}
-- b/b.go --
package b
import "example.com/c"
type B struct{}
func (B) Printf(format string, args ...interface{}) {
c.MyPrintf(format, args...)
}
-- c/c.go --
package c
import "fmt"
func MyPrintf(format string, args ...interface{}) {
fmt.Printf(format, args...)
}
`
Run(t, src, func(t *testing.T, env *Env) {
env.OpenFile("a/a.go")
env.AfterChange(
env.DiagnosticAtRegexpWithMessage("a/a.go", "new.*Printf",
"format %d has arg \"s\" of wrong type string"))
})
}
// This test ensures that only Analyzers with RunDespiteErrors=true
// are invoked on a package that would not compile, even if the errors
// are distant and localized.
func TestErrorsThatPreventAnalysis(t *testing.T) {
const src = `
-- go.mod --
module example.com
-- a/a.go --
package a
import "fmt"
import "sync"
import _ "example.com/b"
func _() {
// The copylocks analyzer (RunDespiteErrors, FactTypes={}) does run.
var mu sync.Mutex
mu2 := mu // copylocks error, reported
_ = &mu2
// The printf analyzer (!RunDespiteErrors, FactTypes!={}) does not run:
// (c, printf) failed because of type error in c
// (b, printf) and (a, printf) do not run because of failed prerequisites.
fmt.Printf("%d", "s") // printf error, unreported
// The bools analyzer (!RunDespiteErrors, FactTypes={}) does not run:
var cond bool
_ = cond != true && cond != true // bools error, unreported
}
-- b/b.go --
package b
import _ "example.com/c"
-- c/c.go --
package c
var _ = 1 / "" // type error
`
Run(t, src, func(t *testing.T, env *Env) {
var diags protocol.PublishDiagnosticsParams
env.OpenFile("a/a.go")
env.AfterChange(
env.DiagnosticAtRegexpWithMessage("a/a.go", "mu2 := (mu)", "assignment copies lock value"),
ReadDiagnostics("a/a.go", &diags))
// Assert that there were no other diagnostics.
// In particular:
// - "fmt.Printf" does not trigger a [printf] finding;
// - "cond != true" does not trigger a [bools] finding.
//
// We use this check in preference to NoDiagnosticAtRegexp
// as it is robust in case of minor mistakes in the position
// regexp, and because it reports unexpected diagnostics.
if got, want := len(diags.Diagnostics), 1; got != want {
t.Errorf("got %d diagnostics in a/a.go, want %d:", got, want)
for i, diag := range diags.Diagnostics {
t.Logf("Diagnostics[%d] = %+v", i, diag)
}
}
})
}
|